diff options
author | Douglas Gregor <dgregor@apple.com> | 2010-11-09 05:28:47 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2010-11-09 05:28:47 +0000 |
commit | d1ea3f0d29dd9e18c42d17ef281c744b5bea6b92 (patch) | |
tree | 3580c7cb3ab4b9e937bd8707cb989419f836ac62 /clang/tools/libclang | |
parent | c1351cac17c2357866132aeaa69706fc84bd4201 (diff) | |
download | bcm5719-llvm-d1ea3f0d29dd9e18c42d17ef281c744b5bea6b92.tar.gz bcm5719-llvm-d1ea3f0d29dd9e18c42d17ef281c744b5bea6b92.zip |
Introduce clang_getSpellingLocation() into libclang, to provide the
location where we're spelling a token even within a
macro. clang_getInstantiationLocation() tells where we instantiated
the macro.
I'm still not thrilled with the CXSourceLocation/CXSourceRange APIs,
since they gloss over macro-instantiation information.
llvm-svn: 118492
Diffstat (limited to 'clang/tools/libclang')
-rw-r--r-- | clang/tools/libclang/CIndex.cpp | 39 | ||||
-rw-r--r-- | clang/tools/libclang/CIndexDiagnostic.cpp | 14 | ||||
-rw-r--r-- | clang/tools/libclang/libclang.darwin.exports | 1 | ||||
-rw-r--r-- | clang/tools/libclang/libclang.exports | 1 |
4 files changed, 47 insertions, 8 deletions
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index 496833c7234..42617f0ce46 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -104,8 +104,9 @@ CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, const CharSourceRange &R) { // We want the last character in this location, so we will adjust the // location accordingly. - // FIXME: How do do this with a macro instantiation location? SourceLocation EndLoc = R.getEnd(); + if (EndLoc.isValid() && EndLoc.isMacroID()) + EndLoc = SM.getSpellingLoc(EndLoc); if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) { unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts); EndLoc = EndLoc.getFileLocWithOffset(Length); @@ -2455,6 +2456,42 @@ void clang_getInstantiationLocation(CXSourceLocation location, *offset = SM.getDecomposedLoc(InstLoc).second; } +void clang_getSpellingLocation(CXSourceLocation location, + CXFile *file, + unsigned *line, + unsigned *column, + unsigned *offset) { + SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); + + if (!location.ptr_data[0] || Loc.isInvalid()) { + if (file) + *file = 0; + if (line) + *line = 0; + if (column) + *column = 0; + if (offset) + *offset = 0; + return; + } + + const SourceManager &SM = + *static_cast<const SourceManager*>(location.ptr_data[0]); + SourceLocation SpellLoc = SM.getSpellingLoc(Loc); + std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc); + FileID FID = LocInfo.first; + unsigned FileOffset = LocInfo.second; + + if (file) + *file = (void *)SM.getFileEntryForID(FID); + if (line) + *line = SM.getLineNumber(FID, FileOffset); + if (column) + *column = SM.getColumnNumber(FID, FileOffset); + if (offset) + *offset = FileOffset; +} + CXSourceLocation clang_getRangeStart(CXSourceRange range) { CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] }, range.begin_int_data }; diff --git a/clang/tools/libclang/CIndexDiagnostic.cpp b/clang/tools/libclang/CIndexDiagnostic.cpp index 0766548418e..036a28b5387 100644 --- a/clang/tools/libclang/CIndexDiagnostic.cpp +++ b/clang/tools/libclang/CIndexDiagnostic.cpp @@ -64,8 +64,8 @@ CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) { // and source ranges. CXFile File; unsigned Line, Column; - clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic), - &File, &Line, &Column, 0); + clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), + &File, &Line, &Column, 0); if (File) { CXString FName = clang_getFileName(File); Out << clang_getCString(FName) << ":" << Line << ":"; @@ -81,11 +81,11 @@ CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) { CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I); unsigned StartLine, StartColumn, EndLine, EndColumn; - clang_getInstantiationLocation(clang_getRangeStart(Range), - &StartFile, &StartLine, &StartColumn, - 0); - clang_getInstantiationLocation(clang_getRangeEnd(Range), - &EndFile, &EndLine, &EndColumn, 0); + clang_getSpellingLocation(clang_getRangeStart(Range), + &StartFile, &StartLine, &StartColumn, + 0); + clang_getSpellingLocation(clang_getRangeEnd(Range), + &EndFile, &EndLine, &EndColumn, 0); if (StartFile != EndFile || StartFile != File) continue; diff --git a/clang/tools/libclang/libclang.darwin.exports b/clang/tools/libclang/libclang.darwin.exports index 7d5ae8b7327..09f3cf7d210 100644 --- a/clang/tools/libclang/libclang.darwin.exports +++ b/clang/tools/libclang/libclang.darwin.exports @@ -88,6 +88,7 @@ _clang_getRangeEnd _clang_getRangeStart _clang_getResultType _clang_getSpecializedCursorTemplate +_clang_getSpellingLocation _clang_getTemplateCursorKind _clang_getTokenExtent _clang_getTokenKind diff --git a/clang/tools/libclang/libclang.exports b/clang/tools/libclang/libclang.exports index da7f9c851fb..6cbd2c63b72 100644 --- a/clang/tools/libclang/libclang.exports +++ b/clang/tools/libclang/libclang.exports @@ -88,6 +88,7 @@ clang_getRangeEnd clang_getRangeStart clang_getResultType clang_getSpecializedCursorTemplate +clang_getSpellingLocation clang_getTemplateCursorKind clang_getTokenExtent clang_getTokenKind |