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"
|
|
|
|
#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) {
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit);
|
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) {
|
|
|
|
ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit);
|
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) {
|
|
|
|
CXStoredDiagnostic *Stored = static_cast<CXStoredDiagnostic *>(Diagnostic);
|
|
|
|
delete Stored;
|
|
|
|
}
|
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) {
|
|
|
|
case CXDiagnostic_Ignored: assert(0 && "impossible"); break;
|
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-02-23 07:17:23 +08:00
|
|
|
return createCXString(Out.str(), true);
|
2010-02-19 07:07:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_defaultDiagnosticDisplayOptions() {
|
|
|
|
return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn;
|
|
|
|
}
|
|
|
|
|
2010-01-28 08:27:43 +08:00
|
|
|
enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
|
|
|
|
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
|
|
|
|
if (!StoredDiag)
|
|
|
|
return CXDiagnostic_Ignored;
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
switch (StoredDiag->Diag.getLevel()) {
|
2010-01-28 08:27:43 +08:00
|
|
|
case Diagnostic::Ignored: return CXDiagnostic_Ignored;
|
|
|
|
case Diagnostic::Note: return CXDiagnostic_Note;
|
|
|
|
case Diagnostic::Warning: return CXDiagnostic_Warning;
|
|
|
|
case Diagnostic::Error: return CXDiagnostic_Error;
|
|
|
|
case Diagnostic::Fatal: return CXDiagnostic_Fatal;
|
|
|
|
}
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-01-28 08:27:43 +08:00
|
|
|
llvm_unreachable("Invalid diagnostic level");
|
|
|
|
return CXDiagnostic_Ignored;
|
|
|
|
}
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-01-28 08:27:43 +08:00
|
|
|
CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) {
|
|
|
|
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
|
2010-02-19 02:08:43 +08:00
|
|
|
if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid())
|
2010-01-28 08:27:43 +08:00
|
|
|
return clang_getNullLocation();
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
return translateSourceLocation(StoredDiag->Diag.getLocation().getManager(),
|
|
|
|
StoredDiag->LangOpts,
|
|
|
|
StoredDiag->Diag.getLocation());
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) {
|
|
|
|
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
|
|
|
|
if (!StoredDiag)
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString("");
|
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
return createCXString(StoredDiag->Diag.getMessage(), false);
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
|
|
|
|
2010-02-09 07:11:56 +08:00
|
|
|
unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
|
|
|
|
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
|
2010-02-19 02:08:43 +08:00
|
|
|
if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid())
|
2010-02-09 07:11:56 +08:00
|
|
|
return 0;
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
return StoredDiag->Diag.range_size();
|
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) {
|
2010-01-28 08:27:43 +08:00
|
|
|
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
|
2010-02-19 02:08:43 +08:00
|
|
|
if (!StoredDiag || Range >= StoredDiag->Diag.range_size() ||
|
|
|
|
StoredDiag->Diag.getLocation().isInvalid())
|
2010-02-09 07:11:56 +08:00
|
|
|
return clang_getNullRange();
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
return translateSourceRange(StoredDiag->Diag.getLocation().getManager(),
|
|
|
|
StoredDiag->LangOpts,
|
|
|
|
StoredDiag->Diag.range_begin()[Range]);
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) {
|
|
|
|
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
|
|
|
|
if (!StoredDiag)
|
|
|
|
return 0;
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
return StoredDiag->Diag.fixit_size();
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
|
|
|
|
2010-02-20 02:16:06 +08:00
|
|
|
CXString clang_getDiagnosticFixIt(CXDiagnostic Diagnostic, unsigned FixIt,
|
|
|
|
CXSourceRange *ReplacementRange) {
|
|
|
|
CXStoredDiagnostic *StoredDiag
|
|
|
|
= static_cast<CXStoredDiagnostic *>(Diagnostic);
|
2010-02-19 02:08:43 +08:00
|
|
|
if (!StoredDiag || FixIt >= StoredDiag->Diag.fixit_size() ||
|
|
|
|
StoredDiag->Diag.getLocation().isInvalid()) {
|
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
|
|
|
}
|
2010-02-17 08:41:08 +08:00
|
|
|
|
2010-04-01 01:46:05 +08:00
|
|
|
const FixItHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt];
|
2010-02-20 02:16:06 +08:00
|
|
|
if (ReplacementRange) {
|
2010-08-18 22:24:02 +08:00
|
|
|
// Create a range that covers the entire replacement (or
|
|
|
|
// removal) range, adjusting the end of the range to point to
|
|
|
|
// the end of the token.
|
|
|
|
*ReplacementRange
|
|
|
|
= translateSourceRange(StoredDiag->Diag.getLocation().getManager(),
|
|
|
|
StoredDiag->LangOpts,
|
|
|
|
Hint.RemoveRange);
|
2010-02-20 02:16:06 +08:00
|
|
|
}
|
|
|
|
|
2010-02-17 08:41:08 +08:00
|
|
|
return createCXString(Hint.CodeToInsert);
|
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"
|