2010-02-19 02:08:43 +08:00
|
|
|
/*===-- CIndexDiagnostics.cpp - Diagnostics C Interface ---------*- C++ -*-===*\
|
2010-01-28 08:27:43 +08:00
|
|
|
|* *|
|
|
|
|
|* The LLVM Compiler Infrastructure *|
|
|
|
|
|* *|
|
|
|
|
|* This file is distributed under the University of Illinois Open Source *|
|
|
|
|
|* License. See LICENSE.TXT for details. *|
|
|
|
|
|* *|
|
|
|
|
|*===----------------------------------------------------------------------===*|
|
|
|
|
|* *|
|
|
|
|
|* Implements the diagnostic functions of the Clang C interface. *|
|
|
|
|
|* *|
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
#include "CIndexDiagnostic.h"
|
|
|
|
#include "CIndexer.h"
|
2010-11-18 07:24:11 +08:00
|
|
|
#include "CXTranslationUnit.h"
|
2010-01-28 08:27:43 +08:00
|
|
|
#include "CXSourceLocation.h"
|
2010-11-16 09:56:27 +08:00
|
|
|
#include "CXString.h"
|
2010-01-28 08:27:43 +08:00
|
|
|
|
2010-04-13 03:45:50 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2010-01-28 14:00:51 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2010-02-23 07:17:23 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-02-19 02:08:43 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2010-01-28 14:00:51 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-02-23 07:17:23 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-01-28 14:00:51 +08:00
|
|
|
|
2010-01-28 08:27:43 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::cxloc;
|
2010-02-17 08:41:08 +08:00
|
|
|
using namespace clang::cxstring;
|
2010-02-19 02:08:43 +08:00
|
|
|
using namespace llvm;
|
2010-01-28 08:27:43 +08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2010-02-19 02:08:43 +08:00
|
|
|
// C Interface Routines
|
2010-01-28 08:27:43 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
2010-02-19 02:08:43 +08:00
|
|
|
extern "C" {
|
2010-01-31 07:31:49 +08:00
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) {
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit->TUData);
|
2010-04-06 02:10:21 +08:00
|
|
|
return CXXUnit? CXXUnit->stored_diag_size() : 0;
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) {
|
2010-11-16 16:15:36 +08:00
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit->TUData);
|
2010-04-06 02:10:21 +08:00
|
|
|
if (!CXXUnit || Index >= CXXUnit->stored_diag_size())
|
2010-02-19 02:08:43 +08:00
|
|
|
return 0;
|
2010-01-31 07:31:49 +08:00
|
|
|
|
2010-04-06 02:10:21 +08:00
|
|
|
return new CXStoredDiagnostic(CXXUnit->stored_diag_begin()[Index],
|
2010-02-19 02:08:43 +08:00
|
|
|
CXXUnit->getASTContext().getLangOptions());
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
void clang_disposeDiagnostic(CXDiagnostic Diagnostic) {
|
2011-11-01 05:40:19 +08:00
|
|
|
delete static_cast<CXDiagnosticImpl *>(Diagnostic);
|
2010-02-19 02:08:43 +08:00
|
|
|
}
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-02-23 07:17:23 +08:00
|
|
|
CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
|
|
|
|
if (!Diagnostic)
|
|
|
|
return createCXString("");
|
2010-02-19 07:07:20 +08:00
|
|
|
|
|
|
|
CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic);
|
|
|
|
|
2010-02-23 07:17:23 +08:00
|
|
|
llvm::SmallString<256> Str;
|
|
|
|
llvm::raw_svector_ostream Out(Str);
|
|
|
|
|
2010-02-19 07:07:20 +08:00
|
|
|
if (Options & CXDiagnostic_DisplaySourceLocation) {
|
|
|
|
// Print source location (file:line), along with optional column
|
|
|
|
// and source ranges.
|
|
|
|
CXFile File;
|
|
|
|
unsigned Line, Column;
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
|
|
|
|
&File, &Line, &Column, 0);
|
2010-02-19 07:07:20 +08:00
|
|
|
if (File) {
|
|
|
|
CXString FName = clang_getFileName(File);
|
2010-02-23 07:17:23 +08:00
|
|
|
Out << clang_getCString(FName) << ":" << Line << ":";
|
2010-02-19 07:07:20 +08:00
|
|
|
clang_disposeString(FName);
|
|
|
|
if (Options & CXDiagnostic_DisplayColumn)
|
2010-02-23 07:17:23 +08:00
|
|
|
Out << Column << ":";
|
2010-02-19 07:07:20 +08:00
|
|
|
|
|
|
|
if (Options & CXDiagnostic_DisplaySourceRanges) {
|
|
|
|
unsigned N = clang_getDiagnosticNumRanges(Diagnostic);
|
|
|
|
bool PrintedRange = false;
|
|
|
|
for (unsigned I = 0; I != N; ++I) {
|
|
|
|
CXFile StartFile, EndFile;
|
|
|
|
CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I);
|
|
|
|
|
|
|
|
unsigned StartLine, StartColumn, EndLine, EndColumn;
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(clang_getRangeStart(Range),
|
|
|
|
&StartFile, &StartLine, &StartColumn,
|
|
|
|
0);
|
|
|
|
clang_getSpellingLocation(clang_getRangeEnd(Range),
|
|
|
|
&EndFile, &EndLine, &EndColumn, 0);
|
2010-02-19 07:07:20 +08:00
|
|
|
|
|
|
|
if (StartFile != EndFile || StartFile != File)
|
|
|
|
continue;
|
|
|
|
|
2010-02-23 07:17:23 +08:00
|
|
|
Out << "{" << StartLine << ":" << StartColumn << "-"
|
|
|
|
<< EndLine << ":" << EndColumn << "}";
|
2010-02-19 07:07:20 +08:00
|
|
|
PrintedRange = true;
|
|
|
|
}
|
|
|
|
if (PrintedRange)
|
2010-02-23 07:17:23 +08:00
|
|
|
Out << ":";
|
2010-02-19 07:07:20 +08:00
|
|
|
}
|
2010-10-12 08:50:20 +08:00
|
|
|
|
|
|
|
Out << " ";
|
2010-02-19 07:07:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print warning/error/etc. */
|
|
|
|
switch (Severity) {
|
2011-09-24 04:26:49 +08:00
|
|
|
case CXDiagnostic_Ignored: llvm_unreachable("impossible");
|
2010-02-23 07:17:23 +08:00
|
|
|
case CXDiagnostic_Note: Out << "note: "; break;
|
|
|
|
case CXDiagnostic_Warning: Out << "warning: "; break;
|
|
|
|
case CXDiagnostic_Error: Out << "error: "; break;
|
|
|
|
case CXDiagnostic_Fatal: Out << "fatal error: "; break;
|
2010-02-19 07:07:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString Text = clang_getDiagnosticSpelling(Diagnostic);
|
|
|
|
if (clang_getCString(Text))
|
2010-02-23 07:17:23 +08:00
|
|
|
Out << clang_getCString(Text);
|
2010-02-19 07:07:20 +08:00
|
|
|
else
|
2010-02-23 07:17:23 +08:00
|
|
|
Out << "<no diagnostic text>";
|
2010-02-19 07:07:20 +08:00
|
|
|
clang_disposeString(Text);
|
2010-11-20 00:18:16 +08:00
|
|
|
|
|
|
|
if (Options & (CXDiagnostic_DisplayOption | CXDiagnostic_DisplayCategoryId |
|
|
|
|
CXDiagnostic_DisplayCategoryName)) {
|
|
|
|
bool NeedBracket = true;
|
|
|
|
bool NeedComma = false;
|
|
|
|
|
|
|
|
if (Options & CXDiagnostic_DisplayOption) {
|
|
|
|
CXString OptionName = clang_getDiagnosticOption(Diagnostic, 0);
|
|
|
|
if (const char *OptionText = clang_getCString(OptionName)) {
|
|
|
|
if (OptionText[0]) {
|
|
|
|
Out << " [" << OptionText;
|
|
|
|
NeedBracket = false;
|
|
|
|
NeedComma = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clang_disposeString(OptionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Options & (CXDiagnostic_DisplayCategoryId |
|
|
|
|
CXDiagnostic_DisplayCategoryName)) {
|
|
|
|
if (unsigned CategoryID = clang_getDiagnosticCategory(Diagnostic)) {
|
|
|
|
if (Options & CXDiagnostic_DisplayCategoryId) {
|
|
|
|
if (NeedBracket)
|
|
|
|
Out << " [";
|
|
|
|
if (NeedComma)
|
|
|
|
Out << ", ";
|
|
|
|
Out << CategoryID;
|
|
|
|
NeedBracket = false;
|
|
|
|
NeedComma = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Options & CXDiagnostic_DisplayCategoryName) {
|
|
|
|
CXString CategoryName = clang_getDiagnosticCategoryName(CategoryID);
|
|
|
|
if (NeedBracket)
|
|
|
|
Out << " [";
|
|
|
|
if (NeedComma)
|
|
|
|
Out << ", ";
|
|
|
|
Out << clang_getCString(CategoryName);
|
|
|
|
NeedBracket = false;
|
|
|
|
NeedComma = true;
|
|
|
|
clang_disposeString(CategoryName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NeedBracket)
|
|
|
|
Out << "]";
|
|
|
|
}
|
|
|
|
|
2010-02-23 07:17:23 +08:00
|
|
|
return createCXString(Out.str(), true);
|
2010-02-19 07:07:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_defaultDiagnosticDisplayOptions() {
|
2010-11-20 00:18:16 +08:00
|
|
|
return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn |
|
|
|
|
CXDiagnostic_DisplayOption;
|
2010-02-19 07:07:20 +08:00
|
|
|
}
|
|
|
|
|
2010-01-28 08:27:43 +08:00
|
|
|
enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
|
2011-11-01 05:40:19 +08:00
|
|
|
if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
|
|
|
|
return D->getSeverity();
|
2010-01-28 08:27:43 +08:00
|
|
|
return CXDiagnostic_Ignored;
|
|
|
|
}
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-01-28 08:27:43 +08:00
|
|
|
CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) {
|
2011-11-01 05:40:19 +08:00
|
|
|
if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
|
|
|
|
return D->getLocation();
|
|
|
|
return clang_getNullLocation();
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) {
|
2011-11-01 05:40:19 +08:00
|
|
|
if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
|
|
|
|
return D->getSpelling();
|
|
|
|
return createCXString("");
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
|
|
|
|
2010-11-20 00:18:16 +08:00
|
|
|
CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) {
|
|
|
|
if (Disable)
|
|
|
|
*Disable = createCXString("");
|
2011-11-01 05:40:19 +08:00
|
|
|
|
|
|
|
if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
|
|
|
|
return D->getDiagnosticOption(Disable);
|
2010-11-20 00:18:16 +08:00
|
|
|
|
|
|
|
return createCXString("");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) {
|
2011-11-01 05:40:19 +08:00
|
|
|
if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
|
|
|
|
return D->getCategory();
|
|
|
|
return 0;
|
2010-11-20 00:18:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_getDiagnosticCategoryName(unsigned Category) {
|
|
|
|
return createCXString(DiagnosticIDs::getCategoryNameFromID(Category));
|
|
|
|
}
|
|
|
|
|
2010-02-09 07:11:56 +08:00
|
|
|
unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
|
2011-11-01 05:40:19 +08:00
|
|
|
if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
|
|
|
|
return D->getNumRanges();
|
|
|
|
return 0;
|
2010-02-09 07:11:56 +08:00
|
|
|
}
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-02-09 07:11:56 +08:00
|
|
|
CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) {
|
2011-11-01 05:40:19 +08:00
|
|
|
CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
|
|
|
|
if (!D || Range >= D->getNumRanges())
|
2010-02-09 07:11:56 +08:00
|
|
|
return clang_getNullRange();
|
2011-11-01 05:40:19 +08:00
|
|
|
return D->getRange(Range);
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) {
|
2011-11-01 05:40:19 +08:00
|
|
|
if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
|
|
|
|
return D->getNumFixIts();
|
|
|
|
return 0;
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
|
|
|
|
2011-11-01 05:40:19 +08:00
|
|
|
CXString clang_getDiagnosticFixIt(CXDiagnostic Diag, unsigned FixIt,
|
2010-02-20 02:16:06 +08:00
|
|
|
CXSourceRange *ReplacementRange) {
|
2011-11-01 05:40:19 +08:00
|
|
|
CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
|
|
|
|
if (!D || FixIt >= D->getNumFixIts()) {
|
2010-02-20 02:16:06 +08:00
|
|
|
if (ReplacementRange)
|
|
|
|
*ReplacementRange = clang_getNullRange();
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString("");
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
2011-11-01 05:40:19 +08:00
|
|
|
return D->getFixIt(FixIt, ReplacementRange);
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-01-28 08:27:43 +08:00
|
|
|
} // end extern "C"
|