diff options
| author | Douglas Gregor <dgregor@apple.com> | 2010-10-20 22:00:55 +0000 |
|---|---|---|
| committer | Douglas Gregor <dgregor@apple.com> | 2010-10-20 22:00:55 +0000 |
| commit | 796d76a663795d66ed5aa97a1f585514dd68b043 (patch) | |
| tree | 98f71284a72e7f7714726b6ac5058e61dc56d97d /clang/tools | |
| parent | 2edaa2fb24eb17fbb6b3752ddb7d06a8d6b0799e (diff) | |
| download | bcm5719-llvm-796d76a663795d66ed5aa97a1f585514dd68b043.tar.gz bcm5719-llvm-796d76a663795d66ed5aa97a1f585514dd68b043.zip | |
Extend the preprocessing record and libclang with support for
inclusion directives, keeping track of every #include, #import,
etc. in the translation unit. We keep track of the source location and
kind of the inclusion, how the file name was spelled, and the
underlying file to which the inclusion resolved.
llvm-svn: 116952
Diffstat (limited to 'clang/tools')
| -rw-r--r-- | clang/tools/c-index-test/c-index-test.c | 7 | ||||
| -rw-r--r-- | clang/tools/libclang/CIndex.cpp | 36 | ||||
| -rw-r--r-- | clang/tools/libclang/CXCursor.cpp | 11 | ||||
| -rw-r--r-- | clang/tools/libclang/CXCursor.h | 8 | ||||
| -rw-r--r-- | clang/tools/libclang/libclang.darwin.exports | 1 | ||||
| -rw-r--r-- | clang/tools/libclang/libclang.exports | 1 |
6 files changed, 60 insertions, 4 deletions
diff --git a/clang/tools/c-index-test/c-index-test.c b/clang/tools/c-index-test/c-index-test.c index 32cada08ae5..c95fb307a69 100644 --- a/clang/tools/c-index-test/c-index-test.c +++ b/clang/tools/c-index-test/c-index-test.c @@ -271,6 +271,13 @@ static void PrintCursor(CXCursor Cursor) { printf("]"); clang_disposeOverriddenCursors(overridden); } + + if (Cursor.kind == CXCursor_InclusionDirective) { + CXFile File = clang_getIncludedFile(Cursor); + CXString Included = clang_getFileName(File); + printf(" (%s)", clang_getCString(Included)); + clang_disposeString(Included); + } } } diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index 722e2cb7381..c944883fa4d 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -555,6 +555,13 @@ bool CursorVisitor::VisitChildren(CXCursor Cursor) { continue; } + + if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) { + if (Visit(MakeInclusionDirectiveCursor(ID, CXXUnit))) + return true; + + continue; + } } } return false; @@ -2565,6 +2572,9 @@ CXString clang_getCursorSpelling(CXCursor C) { return createCXString(getCursorMacroDefinition(C)->getName() ->getNameStart()); + if (C.kind == CXCursor_InclusionDirective) + return createCXString(getCursorInclusionDirective(C)->getFileName()); + if (clang_isDeclaration(C.kind)) return getDeclSpelling(getCursorDecl(C)); @@ -2757,6 +2767,8 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { return createCXString("macro definition"); case CXCursor_MacroInstantiation: return createCXString("macro instantiation"); + case CXCursor_InclusionDirective: + return createCXString("inclusion directive"); case CXCursor_Namespace: return createCXString("Namespace"); case CXCursor_LinkageSpec: @@ -2977,7 +2989,13 @@ CXSourceLocation clang_getCursorLocation(CXCursor C) { SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); return cxloc::translateSourceLocation(getCursorContext(C), L); } - + + if (C.kind == CXCursor_InclusionDirective) { + SourceLocation L + = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); + return cxloc::translateSourceLocation(getCursorContext(C), L); + } + if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) return clang_getNullLocation(); @@ -3043,12 +3061,14 @@ static SourceRange getRawCursorExtent(CXCursor C) { if (C.kind == CXCursor_MacroDefinition) return cxcursor::getCursorMacroDefinition(C)->getSourceRange(); - + + if (C.kind == CXCursor_InclusionDirective) + return cxcursor::getCursorInclusionDirective(C)->getSourceRange(); + if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) return getCursorDecl(C)->getSourceRange(); - return SourceRange(); -} + return SourceRange();} extern "C" { @@ -4149,6 +4169,14 @@ void clang_disposeOverriddenCursors(CXCursor *overridden) { delete [] overridden; } +CXFile clang_getIncludedFile(CXCursor cursor) { + if (cursor.kind != CXCursor_InclusionDirective) + return 0; + + InclusionDirective *ID = getCursorInclusionDirective(cursor); + return (void *)ID->getFile(); +} + } // end: extern "C" diff --git a/clang/tools/libclang/CXCursor.cpp b/clang/tools/libclang/CXCursor.cpp index 52594dcf6b0..d506400407f 100644 --- a/clang/tools/libclang/CXCursor.cpp +++ b/clang/tools/libclang/CXCursor.cpp @@ -363,6 +363,17 @@ MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) { return static_cast<MacroInstantiation *>(C.data[0]); } +CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID, + ASTUnit *TU) { + CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } }; + return C; +} + +InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) { + assert(C.kind == CXCursor_InclusionDirective); + return static_cast<InclusionDirective *>(C.data[0]); +} + CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc, ASTUnit *TU) { diff --git a/clang/tools/libclang/CXCursor.h b/clang/tools/libclang/CXCursor.h index 4a29dd0f1c1..a31a3220767 100644 --- a/clang/tools/libclang/CXCursor.h +++ b/clang/tools/libclang/CXCursor.h @@ -28,6 +28,7 @@ class CXXBaseSpecifier; class Decl; class Expr; class FieldDecl; +class InclusionDirective; class LabelStmt; class MacroDefinition; class MacroInstantiation; @@ -133,6 +134,13 @@ CXCursor MakeMacroInstantiationCursor(MacroInstantiation *, ASTUnit *TU); /// source range. MacroInstantiation *getCursorMacroInstantiation(CXCursor C); +/// \brief Create an inclusion directive cursor. +CXCursor MakeInclusionDirectiveCursor(InclusionDirective *, ASTUnit *TU); + +/// \brief Unpack a given inclusion directive cursor to retrieve its +/// source range. +InclusionDirective *getCursorInclusionDirective(CXCursor C); + /// \brief Create a label reference at the given location. CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc, ASTUnit *TU); diff --git a/clang/tools/libclang/libclang.darwin.exports b/clang/tools/libclang/libclang.darwin.exports index dbb335f9aa7..e9d40d0a754 100644 --- a/clang/tools/libclang/libclang.darwin.exports +++ b/clang/tools/libclang/libclang.darwin.exports @@ -68,6 +68,7 @@ _clang_getFile _clang_getFileName _clang_getFileTime _clang_getIBOutletCollectionType +_clang_getIncludedFile _clang_getInclusions _clang_getInstantiationLocation _clang_getLocation diff --git a/clang/tools/libclang/libclang.exports b/clang/tools/libclang/libclang.exports index 06e5a8bf517..9935a584b9b 100644 --- a/clang/tools/libclang/libclang.exports +++ b/clang/tools/libclang/libclang.exports @@ -68,6 +68,7 @@ clang_getFile clang_getFileName clang_getFileTime clang_getIBOutletCollectionType +clang_getIncludedFile clang_getInclusions clang_getInstantiationLocation clang_getLocation |

