Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1 | //===- CIndexHigh.cpp - Higher level API functions ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "IndexingContext.h" |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 11 | #include "CIndexDiagnostic.h" |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 12 | #include "CIndexer.h" |
Chandler Carruth | b1ba0ef | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 13 | #include "CLog.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 14 | #include "CXCursor.h" |
| 15 | #include "CXSourceLocation.h" |
| 16 | #include "CXString.h" |
| 17 | #include "CXTranslationUnit.h" |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTConsumer.h" |
| 19 | #include "clang/AST/DeclVisitor.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/ASTUnit.h" |
| 21 | #include "clang/Frontend/CompilerInstance.h" |
| 22 | #include "clang/Frontend/CompilerInvocation.h" |
| 23 | #include "clang/Frontend/FrontendAction.h" |
| 24 | #include "clang/Frontend/Utils.h" |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 25 | #include "clang/Lex/HeaderSearch.h" |
Chandler Carruth | b99083e | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 26 | #include "clang/Lex/PPCallbacks.h" |
| 27 | #include "clang/Lex/PPConditionalDirectiveRecord.h" |
| 28 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 29 | #include "clang/Sema/SemaConsumer.h" |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CrashRecoveryContext.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MemoryBuffer.h" |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Mutex.h" |
| 33 | #include "llvm/Support/MutexGuard.h" |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace clang; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 36 | using namespace cxtu; |
| 37 | using namespace cxindex; |
| 38 | |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 39 | static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx); |
| 40 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 41 | namespace { |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 44 | // Skip Parsed Bodies |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
| 47 | #ifdef LLVM_ON_WIN32 |
| 48 | |
| 49 | // FIXME: On windows it is disabled since current implementation depends on |
| 50 | // file inodes. |
| 51 | |
| 52 | class SessionSkipBodyData { }; |
| 53 | |
| 54 | class TUSkipBodyControl { |
| 55 | public: |
| 56 | TUSkipBodyControl(SessionSkipBodyData &sessionData, |
Evgeniy Stepanov | 335078b | 2013-01-31 14:27:38 +0000 | [diff] [blame] | 57 | PPConditionalDirectiveRecord &ppRec, |
| 58 | Preprocessor &pp) { } |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 59 | bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) { |
| 60 | return false; |
| 61 | } |
| 62 | void finished() { } |
| 63 | }; |
| 64 | |
| 65 | #else |
| 66 | |
| 67 | /// \brief A "region" in source code identified by the file/offset of the |
| 68 | /// preprocessor conditional directive that it belongs to. |
| 69 | /// Multiple, non-consecutive ranges can be parts of the same region. |
| 70 | /// |
| 71 | /// As an example of different regions separated by preprocessor directives: |
| 72 | /// |
| 73 | /// \code |
| 74 | /// #1 |
| 75 | /// #ifdef BLAH |
| 76 | /// #2 |
| 77 | /// #ifdef CAKE |
| 78 | /// #3 |
| 79 | /// #endif |
| 80 | /// #2 |
| 81 | /// #endif |
| 82 | /// #1 |
| 83 | /// \endcode |
| 84 | /// |
| 85 | /// There are 3 regions, with non-consecutive parts: |
| 86 | /// #1 is identified as the beginning of the file |
| 87 | /// #2 is identified as the location of "#ifdef BLAH" |
| 88 | /// #3 is identified as the location of "#ifdef CAKE" |
| 89 | /// |
| 90 | class PPRegion { |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 91 | llvm::sys::fs::UniqueID UniqueID; |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 92 | time_t ModTime; |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 93 | unsigned Offset; |
| 94 | public: |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 95 | PPRegion() : ModTime(), Offset() {} |
| 96 | PPRegion(llvm::sys::fs::UniqueID UniqueID, unsigned offset, time_t modTime) |
| 97 | : UniqueID(UniqueID), ModTime(modTime), Offset(offset) {} |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 98 | |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 99 | const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; } |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 100 | unsigned getOffset() const { return Offset; } |
| 101 | time_t getModTime() const { return ModTime; } |
| 102 | |
| 103 | bool isInvalid() const { return *this == PPRegion(); } |
| 104 | |
| 105 | friend bool operator==(const PPRegion &lhs, const PPRegion &rhs) { |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 106 | return lhs.UniqueID == rhs.UniqueID && lhs.Offset == rhs.Offset && |
| 107 | lhs.ModTime == rhs.ModTime; |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 108 | } |
| 109 | }; |
| 110 | |
| 111 | typedef llvm::DenseSet<PPRegion> PPRegionSetTy; |
| 112 | |
| 113 | } // end anonymous namespace |
| 114 | |
| 115 | namespace llvm { |
| 116 | template <> struct isPodLike<PPRegion> { |
| 117 | static const bool value = true; |
| 118 | }; |
| 119 | |
| 120 | template <> |
| 121 | struct DenseMapInfo<PPRegion> { |
| 122 | static inline PPRegion getEmptyKey() { |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 123 | return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-1), 0); |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 124 | } |
| 125 | static inline PPRegion getTombstoneKey() { |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 126 | return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-2), 0); |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static unsigned getHashValue(const PPRegion &S) { |
| 130 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 131 | const llvm::sys::fs::UniqueID &UniqueID = S.getUniqueID(); |
| 132 | ID.AddInteger(UniqueID.getFile()); |
| 133 | ID.AddInteger(UniqueID.getDevice()); |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 134 | ID.AddInteger(S.getOffset()); |
| 135 | ID.AddInteger(S.getModTime()); |
| 136 | return ID.ComputeHash(); |
| 137 | } |
| 138 | |
| 139 | static bool isEqual(const PPRegion &LHS, const PPRegion &RHS) { |
| 140 | return LHS == RHS; |
| 141 | } |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | namespace { |
| 146 | |
| 147 | class SessionSkipBodyData { |
| 148 | llvm::sys::Mutex Mux; |
| 149 | PPRegionSetTy ParsedRegions; |
| 150 | |
| 151 | public: |
| 152 | SessionSkipBodyData() : Mux(/*recursive=*/false) {} |
| 153 | ~SessionSkipBodyData() { |
| 154 | //llvm::errs() << "RegionData: " << Skipped.size() << " - " << Skipped.getMemorySize() << "\n"; |
| 155 | } |
| 156 | |
| 157 | void copyTo(PPRegionSetTy &Set) { |
| 158 | llvm::MutexGuard MG(Mux); |
| 159 | Set = ParsedRegions; |
| 160 | } |
| 161 | |
| 162 | void update(ArrayRef<PPRegion> Regions) { |
| 163 | llvm::MutexGuard MG(Mux); |
| 164 | ParsedRegions.insert(Regions.begin(), Regions.end()); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | class TUSkipBodyControl { |
| 169 | SessionSkipBodyData &SessionData; |
| 170 | PPConditionalDirectiveRecord &PPRec; |
| 171 | Preprocessor &PP; |
| 172 | |
| 173 | PPRegionSetTy ParsedRegions; |
| 174 | SmallVector<PPRegion, 32> NewParsedRegions; |
| 175 | PPRegion LastRegion; |
| 176 | bool LastIsParsed; |
| 177 | |
| 178 | public: |
| 179 | TUSkipBodyControl(SessionSkipBodyData &sessionData, |
| 180 | PPConditionalDirectiveRecord &ppRec, |
| 181 | Preprocessor &pp) |
| 182 | : SessionData(sessionData), PPRec(ppRec), PP(pp) { |
| 183 | SessionData.copyTo(ParsedRegions); |
| 184 | } |
| 185 | |
| 186 | bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) { |
| 187 | PPRegion region = getRegion(Loc, FID, FE); |
| 188 | if (region.isInvalid()) |
| 189 | return false; |
| 190 | |
| 191 | // Check common case, consecutive functions in the same region. |
| 192 | if (LastRegion == region) |
| 193 | return LastIsParsed; |
| 194 | |
| 195 | LastRegion = region; |
| 196 | LastIsParsed = ParsedRegions.count(region); |
| 197 | if (!LastIsParsed) |
| 198 | NewParsedRegions.push_back(region); |
| 199 | return LastIsParsed; |
| 200 | } |
| 201 | |
| 202 | void finished() { |
| 203 | SessionData.update(NewParsedRegions); |
| 204 | } |
| 205 | |
| 206 | private: |
| 207 | PPRegion getRegion(SourceLocation Loc, FileID FID, const FileEntry *FE) { |
| 208 | SourceLocation RegionLoc = PPRec.findConditionalDirectiveRegionLoc(Loc); |
| 209 | if (RegionLoc.isInvalid()) { |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 210 | if (isParsedOnceInclude(FE)) { |
| 211 | const llvm::sys::fs::UniqueID &ID = FE->getUniqueID(); |
| 212 | return PPRegion(ID, 0, FE->getModificationTime()); |
| 213 | } |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 214 | return PPRegion(); |
| 215 | } |
| 216 | |
| 217 | const SourceManager &SM = PPRec.getSourceManager(); |
| 218 | assert(RegionLoc.isFileID()); |
| 219 | FileID RegionFID; |
| 220 | unsigned RegionOffset; |
| 221 | llvm::tie(RegionFID, RegionOffset) = SM.getDecomposedLoc(RegionLoc); |
| 222 | |
| 223 | if (RegionFID != FID) { |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 224 | if (isParsedOnceInclude(FE)) { |
| 225 | const llvm::sys::fs::UniqueID &ID = FE->getUniqueID(); |
| 226 | return PPRegion(ID, 0, FE->getModificationTime()); |
| 227 | } |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 228 | return PPRegion(); |
| 229 | } |
| 230 | |
Rafael Espindola | 0fda0f7 | 2013-08-01 21:42:11 +0000 | [diff] [blame^] | 231 | const llvm::sys::fs::UniqueID &ID = FE->getUniqueID(); |
| 232 | return PPRegion(ID, RegionOffset, FE->getModificationTime()); |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | bool isParsedOnceInclude(const FileEntry *FE) { |
| 236 | return PP.getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE); |
| 237 | } |
| 238 | }; |
| 239 | |
| 240 | #endif |
| 241 | |
| 242 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 243 | // IndexPPCallbacks |
| 244 | //===----------------------------------------------------------------------===// |
| 245 | |
| 246 | class IndexPPCallbacks : public PPCallbacks { |
| 247 | Preprocessor &PP; |
| 248 | IndexingContext &IndexCtx; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 249 | bool IsMainFileEntered; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 250 | |
| 251 | public: |
| 252 | IndexPPCallbacks(Preprocessor &PP, IndexingContext &indexCtx) |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 253 | : PP(PP), IndexCtx(indexCtx), IsMainFileEntered(false) { } |
| 254 | |
| 255 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 256 | SrcMgr::CharacteristicKind FileType, FileID PrevFID) { |
| 257 | if (IsMainFileEntered) |
| 258 | return; |
| 259 | |
| 260 | SourceManager &SM = PP.getSourceManager(); |
| 261 | SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID()); |
| 262 | |
| 263 | if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) { |
| 264 | IsMainFileEntered = true; |
| 265 | IndexCtx.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID())); |
| 266 | } |
| 267 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 268 | |
| 269 | virtual void InclusionDirective(SourceLocation HashLoc, |
| 270 | const Token &IncludeTok, |
| 271 | StringRef FileName, |
| 272 | bool IsAngled, |
Argyrios Kyrtzidis | da31359 | 2012-09-27 01:42:07 +0000 | [diff] [blame] | 273 | CharSourceRange FilenameRange, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 274 | const FileEntry *File, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 275 | StringRef SearchPath, |
Argyrios Kyrtzidis | f8afcff | 2012-09-29 01:06:10 +0000 | [diff] [blame] | 276 | StringRef RelativePath, |
| 277 | const Module *Imported) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 278 | bool isImport = (IncludeTok.is(tok::identifier) && |
| 279 | IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import); |
Argyrios Kyrtzidis | 8d7a24e | 2012-10-18 00:17:05 +0000 | [diff] [blame] | 280 | IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled, |
| 281 | Imported); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /// MacroDefined - This hook is called whenever a macro definition is seen. |
Argyrios Kyrtzidis | c515978 | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 285 | virtual void MacroDefined(const Token &Id, const MacroDirective *MD) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /// MacroUndefined - This hook is called whenever a macro #undef is seen. |
| 289 | /// MI is released immediately following this callback. |
Argyrios Kyrtzidis | c515978 | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 290 | virtual void MacroUndefined(const Token &MacroNameTok, |
| 291 | const MacroDirective *MD) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /// MacroExpands - This is called by when a macro invocation is found. |
Argyrios Kyrtzidis | c515978 | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 295 | virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD, |
Argyrios Kyrtzidis | dd08a0c | 2013-05-03 22:31:32 +0000 | [diff] [blame] | 296 | SourceRange Range, const MacroArgs *Args) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 297 | } |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 298 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 299 | /// SourceRangeSkipped - This hook is called when a source range is skipped. |
| 300 | /// \param Range The SourceRange that was skipped. The range begins at the |
| 301 | /// #if/#else directive and ends after the #endif/#else directive. |
| 302 | virtual void SourceRangeSkipped(SourceRange Range) { |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | //===----------------------------------------------------------------------===// |
| 307 | // IndexingConsumer |
| 308 | //===----------------------------------------------------------------------===// |
| 309 | |
| 310 | class IndexingConsumer : public ASTConsumer { |
| 311 | IndexingContext &IndexCtx; |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 312 | TUSkipBodyControl *SKCtrl; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 313 | |
| 314 | public: |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 315 | IndexingConsumer(IndexingContext &indexCtx, TUSkipBodyControl *skCtrl) |
| 316 | : IndexCtx(indexCtx), SKCtrl(skCtrl) { } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 317 | |
| 318 | // ASTConsumer Implementation |
| 319 | |
| 320 | virtual void Initialize(ASTContext &Context) { |
| 321 | IndexCtx.setASTContext(Context); |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 322 | IndexCtx.startedTranslationUnit(); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 326 | if (SKCtrl) |
| 327 | SKCtrl->finished(); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 330 | virtual bool HandleTopLevelDecl(DeclGroupRef DG) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 331 | IndexCtx.indexDeclGroupRef(DG); |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 332 | return !IndexCtx.shouldAbort(); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /// \brief Handle the specified top-level declaration that occurred inside |
| 336 | /// and ObjC container. |
| 337 | virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) { |
| 338 | // They will be handled after the interface is seen first. |
| 339 | IndexCtx.addTUDeclInObjCContainer(D); |
| 340 | } |
| 341 | |
| 342 | /// \brief This is called by the AST reader when deserializing things. |
| 343 | /// The default implementation forwards to HandleTopLevelDecl but we don't |
| 344 | /// care about them when indexing, so have an empty definition. |
| 345 | virtual void HandleInterestingDecl(DeclGroupRef D) {} |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 346 | |
| 347 | virtual void HandleTagDeclDefinition(TagDecl *D) { |
Argyrios Kyrtzidis | 58d2dbe | 2012-02-14 22:23:11 +0000 | [diff] [blame] | 348 | if (!IndexCtx.shouldIndexImplicitTemplateInsts()) |
| 349 | return; |
| 350 | |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 351 | if (IndexCtx.isTemplateImplicitInstantiation(D)) |
| 352 | IndexCtx.indexDecl(D); |
| 353 | } |
| 354 | |
| 355 | virtual void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) { |
Argyrios Kyrtzidis | 58d2dbe | 2012-02-14 22:23:11 +0000 | [diff] [blame] | 356 | if (!IndexCtx.shouldIndexImplicitTemplateInsts()) |
| 357 | return; |
| 358 | |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 359 | IndexCtx.indexDecl(D); |
| 360 | } |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 361 | |
| 362 | virtual bool shouldSkipFunctionBody(Decl *D) { |
| 363 | if (!SKCtrl) { |
| 364 | // Always skip bodies. |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | const SourceManager &SM = IndexCtx.getASTContext().getSourceManager(); |
| 369 | SourceLocation Loc = D->getLocation(); |
| 370 | if (Loc.isMacroID()) |
| 371 | return false; |
| 372 | if (SM.isInSystemHeader(Loc)) |
| 373 | return true; // always skip bodies from system headers. |
| 374 | |
| 375 | FileID FID; |
| 376 | unsigned Offset; |
| 377 | llvm::tie(FID, Offset) = SM.getDecomposedLoc(Loc); |
| 378 | // Don't skip bodies from main files; this may be revisited. |
| 379 | if (SM.getMainFileID() == FID) |
| 380 | return false; |
| 381 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 382 | if (!FE) |
| 383 | return false; |
| 384 | |
| 385 | return SKCtrl->isParsed(Loc, FID, FE); |
| 386 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 390 | // CaptureDiagnosticConsumer |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 391 | //===----------------------------------------------------------------------===// |
| 392 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 393 | class CaptureDiagnosticConsumer : public DiagnosticConsumer { |
| 394 | SmallVector<StoredDiagnostic, 4> Errors; |
| 395 | public: |
| 396 | |
| 397 | virtual void HandleDiagnostic(DiagnosticsEngine::Level level, |
| 398 | const Diagnostic &Info) { |
| 399 | if (level >= DiagnosticsEngine::Error) |
| 400 | Errors.push_back(StoredDiagnostic(level, Info)); |
| 401 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 402 | }; |
| 403 | |
| 404 | //===----------------------------------------------------------------------===// |
| 405 | // IndexingFrontendAction |
| 406 | //===----------------------------------------------------------------------===// |
| 407 | |
| 408 | class IndexingFrontendAction : public ASTFrontendAction { |
| 409 | IndexingContext IndexCtx; |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 410 | CXTranslationUnit CXTU; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 411 | |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 412 | SessionSkipBodyData *SKData; |
| 413 | OwningPtr<TUSkipBodyControl> SKCtrl; |
| 414 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 415 | public: |
| 416 | IndexingFrontendAction(CXClientData clientData, |
| 417 | IndexerCallbacks &indexCallbacks, |
| 418 | unsigned indexOptions, |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 419 | CXTranslationUnit cxTU, |
| 420 | SessionSkipBodyData *skData) |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 421 | : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU), |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 422 | CXTU(cxTU), SKData(skData) { } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 423 | |
| 424 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 425 | StringRef InFile) { |
Argyrios Kyrtzidis | 3da76bf | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 426 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 427 | |
Argyrios Kyrtzidis | 3da76bf | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 428 | if (!PPOpts.ImplicitPCHInclude.empty()) { |
| 429 | IndexCtx.importedPCH( |
| 430 | CI.getFileManager().getFile(PPOpts.ImplicitPCHInclude)); |
| 431 | } |
Argyrios Kyrtzidis | 3fe9778 | 2012-10-02 16:10:41 +0000 | [diff] [blame] | 432 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 433 | IndexCtx.setASTContext(CI.getASTContext()); |
| 434 | Preprocessor &PP = CI.getPreprocessor(); |
| 435 | PP.addPPCallbacks(new IndexPPCallbacks(PP, IndexCtx)); |
Argyrios Kyrtzidis | 7fe90f3 | 2012-01-17 18:48:07 +0000 | [diff] [blame] | 436 | IndexCtx.setPreprocessor(PP); |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 437 | |
| 438 | if (SKData) { |
| 439 | PPConditionalDirectiveRecord * |
| 440 | PPRec = new PPConditionalDirectiveRecord(PP.getSourceManager()); |
| 441 | PP.addPPCallbacks(PPRec); |
| 442 | SKCtrl.reset(new TUSkipBodyControl(*SKData, *PPRec, PP)); |
| 443 | } |
| 444 | |
| 445 | return new IndexingConsumer(IndexCtx, SKCtrl.get()); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 448 | virtual void EndSourceFileAction() { |
| 449 | indexDiagnostics(CXTU, IndexCtx); |
| 450 | } |
| 451 | |
Argyrios Kyrtzidis | 58d2dbe | 2012-02-14 22:23:11 +0000 | [diff] [blame] | 452 | virtual TranslationUnitKind getTranslationUnitKind() { |
| 453 | if (IndexCtx.shouldIndexImplicitTemplateInsts()) |
| 454 | return TU_Complete; |
| 455 | else |
| 456 | return TU_Prefix; |
| 457 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 458 | virtual bool hasCodeCompletionSupport() const { return false; } |
| 459 | }; |
| 460 | |
| 461 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 462 | // clang_indexSourceFileUnit Implementation |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 463 | //===----------------------------------------------------------------------===// |
| 464 | |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 465 | struct IndexSessionData { |
| 466 | CXIndex CIdx; |
| 467 | OwningPtr<SessionSkipBodyData> SkipBodyData; |
| 468 | |
| 469 | explicit IndexSessionData(CXIndex cIdx) |
| 470 | : CIdx(cIdx), SkipBodyData(new SessionSkipBodyData) {} |
| 471 | }; |
| 472 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 473 | struct IndexSourceFileInfo { |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 474 | CXIndexAction idxAction; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 475 | CXClientData client_data; |
| 476 | IndexerCallbacks *index_callbacks; |
| 477 | unsigned index_callbacks_size; |
| 478 | unsigned index_options; |
| 479 | const char *source_filename; |
| 480 | const char *const *command_line_args; |
| 481 | int num_command_line_args; |
| 482 | struct CXUnsavedFile *unsaved_files; |
| 483 | unsigned num_unsaved_files; |
| 484 | CXTranslationUnit *out_TU; |
| 485 | unsigned TU_options; |
| 486 | int result; |
| 487 | }; |
| 488 | |
| 489 | struct MemBufferOwner { |
| 490 | SmallVector<const llvm::MemoryBuffer *, 8> Buffers; |
| 491 | |
| 492 | ~MemBufferOwner() { |
| 493 | for (SmallVectorImpl<const llvm::MemoryBuffer *>::iterator |
| 494 | I = Buffers.begin(), E = Buffers.end(); I != E; ++I) |
| 495 | delete *I; |
| 496 | } |
| 497 | }; |
| 498 | |
| 499 | } // anonymous namespace |
| 500 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 501 | static void clang_indexSourceFile_Impl(void *UserData) { |
| 502 | IndexSourceFileInfo *ITUI = |
| 503 | static_cast<IndexSourceFileInfo*>(UserData); |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 504 | CXIndexAction cxIdxAction = ITUI->idxAction; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 505 | CXClientData client_data = ITUI->client_data; |
| 506 | IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks; |
| 507 | unsigned index_callbacks_size = ITUI->index_callbacks_size; |
| 508 | unsigned index_options = ITUI->index_options; |
| 509 | const char *source_filename = ITUI->source_filename; |
| 510 | const char * const *command_line_args = ITUI->command_line_args; |
| 511 | int num_command_line_args = ITUI->num_command_line_args; |
| 512 | struct CXUnsavedFile *unsaved_files = ITUI->unsaved_files; |
| 513 | unsigned num_unsaved_files = ITUI->num_unsaved_files; |
| 514 | CXTranslationUnit *out_TU = ITUI->out_TU; |
| 515 | unsigned TU_options = ITUI->TU_options; |
| 516 | ITUI->result = 1; // init as error. |
| 517 | |
| 518 | if (out_TU) |
| 519 | *out_TU = 0; |
| 520 | bool requestedToGetTU = (out_TU != 0); |
| 521 | |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 522 | if (!cxIdxAction) |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 523 | return; |
| 524 | if (!client_index_callbacks || index_callbacks_size == 0) |
| 525 | return; |
| 526 | |
| 527 | IndexerCallbacks CB; |
| 528 | memset(&CB, 0, sizeof(CB)); |
| 529 | unsigned ClientCBSize = index_callbacks_size < sizeof(CB) |
| 530 | ? index_callbacks_size : sizeof(CB); |
| 531 | memcpy(&CB, client_index_callbacks, ClientCBSize); |
| 532 | |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 533 | IndexSessionData *IdxSession = static_cast<IndexSessionData *>(cxIdxAction); |
| 534 | CIndexer *CXXIdx = static_cast<CIndexer *>(IdxSession->CIdx); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 535 | |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 536 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
Argyrios Kyrtzidis | 81b5ac3 | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 537 | setThreadBackgroundPriority(); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 538 | |
Argyrios Kyrtzidis | c58b756 | 2013-03-29 21:51:44 +0000 | [diff] [blame] | 539 | bool CaptureDiagnostics = !Logger::isLoggingEnabled(); |
| 540 | |
| 541 | CaptureDiagnosticConsumer *CaptureDiag = 0; |
| 542 | if (CaptureDiagnostics) |
| 543 | CaptureDiag = new CaptureDiagnosticConsumer(); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 544 | |
| 545 | // Configure the diagnostics. |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 546 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 547 | Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions, |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 548 | CaptureDiag, |
Douglas Gregor | cc2b653 | 2013-05-03 23:07:45 +0000 | [diff] [blame] | 549 | /*ShouldOwnClient=*/true)); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 550 | |
| 551 | // Recover resources if we crash before exiting this function. |
| 552 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 553 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
| 554 | DiagCleanup(Diags.getPtr()); |
| 555 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 556 | OwningPtr<std::vector<const char *> > |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 557 | Args(new std::vector<const char*>()); |
| 558 | |
| 559 | // Recover resources if we crash before exiting this method. |
| 560 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 561 | ArgsCleanup(Args.get()); |
| 562 | |
| 563 | Args->insert(Args->end(), command_line_args, |
| 564 | command_line_args + num_command_line_args); |
| 565 | |
| 566 | // The 'source_filename' argument is optional. If the caller does not |
| 567 | // specify it then it is assumed that the source file is specified |
| 568 | // in the actual argument list. |
| 569 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 570 | // present it will be unused. |
| 571 | if (source_filename) |
| 572 | Args->push_back(source_filename); |
| 573 | |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 574 | IntrusiveRefCntPtr<CompilerInvocation> |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 575 | CInvok(createInvocationFromCommandLine(*Args, Diags)); |
| 576 | |
| 577 | if (!CInvok) |
| 578 | return; |
| 579 | |
| 580 | // Recover resources if we crash before exiting this function. |
| 581 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation, |
| 582 | llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> > |
| 583 | CInvokCleanup(CInvok.getPtr()); |
| 584 | |
| 585 | if (CInvok->getFrontendOpts().Inputs.empty()) |
| 586 | return; |
| 587 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 588 | OwningPtr<MemBufferOwner> BufOwner(new MemBufferOwner()); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 589 | |
| 590 | // Recover resources if we crash before exiting this method. |
| 591 | llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> |
| 592 | BufOwnerCleanup(BufOwner.get()); |
| 593 | |
| 594 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
| 595 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
| 596 | const llvm::MemoryBuffer *Buffer |
| 597 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
| 598 | CInvok->getPreprocessorOpts().addRemappedFile(unsaved_files[I].Filename, Buffer); |
| 599 | BufOwner->Buffers.push_back(Buffer); |
| 600 | } |
| 601 | |
| 602 | // Since libclang is primarily used by batch tools dealing with |
| 603 | // (often very broken) source code, where spell-checking can have a |
| 604 | // significant negative impact on performance (particularly when |
| 605 | // precompiled headers are involved), we disable it. |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 606 | CInvok->getLangOpts()->SpellChecking = false; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 607 | |
Argyrios Kyrtzidis | b49a29f | 2012-03-27 21:38:03 +0000 | [diff] [blame] | 608 | if (index_options & CXIndexOpt_SuppressWarnings) |
| 609 | CInvok->getDiagnosticOpts().IgnoreWarnings = true; |
| 610 | |
Argyrios Kyrtzidis | 991bf49 | 2011-11-28 04:55:55 +0000 | [diff] [blame] | 611 | ASTUnit *Unit = ASTUnit::create(CInvok.getPtr(), Diags, |
Argyrios Kyrtzidis | c58b756 | 2013-03-29 21:51:44 +0000 | [diff] [blame] | 612 | CaptureDiagnostics, |
Argyrios Kyrtzidis | ff39896 | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 613 | /*UserFilesAreVolatile=*/true); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 614 | OwningPtr<CXTUOwner> CXTU(new CXTUOwner(MakeCXTranslationUnit(CXXIdx, Unit))); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 615 | |
| 616 | // Recover resources if we crash before exiting this method. |
| 617 | llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner> |
| 618 | CXTUCleanup(CXTU.get()); |
| 619 | |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 620 | // Enable the skip-parsed-bodies optimization only for C++; this may be |
| 621 | // revisited. |
| 622 | bool SkipBodies = (index_options & CXIndexOpt_SkipParsedBodiesInSession) && |
| 623 | CInvok->getLangOpts()->CPlusPlus; |
| 624 | if (SkipBodies) |
| 625 | CInvok->getFrontendOpts().SkipFunctionBodies = true; |
| 626 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 627 | OwningPtr<IndexingFrontendAction> IndexAction; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 628 | IndexAction.reset(new IndexingFrontendAction(client_data, CB, |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 629 | index_options, CXTU->getTU(), |
| 630 | SkipBodies ? IdxSession->SkipBodyData.get() : 0)); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 631 | |
| 632 | // Recover resources if we crash before exiting this method. |
| 633 | llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction> |
| 634 | IndexActionCleanup(IndexAction.get()); |
| 635 | |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 636 | bool Persistent = requestedToGetTU; |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 637 | bool OnlyLocalDecls = false; |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 638 | bool PrecompilePreamble = false; |
| 639 | bool CacheCodeCompletionResults = false; |
| 640 | PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts(); |
Argyrios Kyrtzidis | bef35c9 | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 641 | PPOpts.AllowPCHWithCompilerErrors = true; |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 642 | |
| 643 | if (requestedToGetTU) { |
| 644 | OnlyLocalDecls = CXXIdx->getOnlyLocalDecls(); |
| 645 | PrecompilePreamble = TU_options & CXTranslationUnit_PrecompiledPreamble; |
| 646 | // FIXME: Add a flag for modules. |
| 647 | CacheCodeCompletionResults |
| 648 | = TU_options & CXTranslationUnit_CacheCompletionResults; |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Argyrios Kyrtzidis | 53d6ded | 2012-10-06 01:18:35 +0000 | [diff] [blame] | 651 | if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 652 | PPOpts.DetailedRecord = true; |
| 653 | } |
Argyrios Kyrtzidis | 3fe9778 | 2012-10-02 16:10:41 +0000 | [diff] [blame] | 654 | |
Argyrios Kyrtzidis | 93cd6e8 | 2012-10-10 02:12:50 +0000 | [diff] [blame] | 655 | if (!requestedToGetTU && !CInvok->getLangOpts()->Modules) |
| 656 | PPOpts.DetailedRecord = false; |
| 657 | |
Argyrios Kyrtzidis | e722ed6 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 658 | DiagnosticErrorTrap DiagTrap(*Diags); |
| 659 | bool Success = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 660 | IndexAction.get(), |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 661 | Unit, |
| 662 | Persistent, |
Argyrios Kyrtzidis | 3791814 | 2012-06-28 21:03:08 +0000 | [diff] [blame] | 663 | CXXIdx->getClangResourcesPath(), |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 664 | OnlyLocalDecls, |
Argyrios Kyrtzidis | c58b756 | 2013-03-29 21:51:44 +0000 | [diff] [blame] | 665 | CaptureDiagnostics, |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 666 | PrecompilePreamble, |
Argyrios Kyrtzidis | ff39896 | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 667 | CacheCodeCompletionResults, |
| 668 | /*IncludeBriefCommentsInCodeCompletion=*/false, |
| 669 | /*UserFilesAreVolatile=*/true); |
Argyrios Kyrtzidis | e722ed6 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 670 | if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics()) |
| 671 | printDiagsToStderr(Unit); |
| 672 | |
| 673 | if (!Success) |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 674 | return; |
| 675 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 676 | if (out_TU) |
| 677 | *out_TU = CXTU->takeTU(); |
| 678 | |
| 679 | ITUI->result = 0; // success. |
| 680 | } |
| 681 | |
| 682 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 683 | // clang_indexTranslationUnit Implementation |
| 684 | //===----------------------------------------------------------------------===// |
| 685 | |
| 686 | namespace { |
| 687 | |
| 688 | struct IndexTranslationUnitInfo { |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 689 | CXIndexAction idxAction; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 690 | CXClientData client_data; |
| 691 | IndexerCallbacks *index_callbacks; |
| 692 | unsigned index_callbacks_size; |
| 693 | unsigned index_options; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 694 | CXTranslationUnit TU; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 695 | int result; |
| 696 | }; |
| 697 | |
| 698 | } // anonymous namespace |
| 699 | |
| 700 | static void indexPreprocessingRecord(ASTUnit &Unit, IndexingContext &IdxCtx) { |
| 701 | Preprocessor &PP = Unit.getPreprocessor(); |
| 702 | if (!PP.getPreprocessingRecord()) |
| 703 | return; |
| 704 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 705 | // FIXME: Only deserialize inclusion directives. |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 706 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 707 | PreprocessingRecord::iterator I, E; |
Argyrios Kyrtzidis | 632dcc9 | 2012-10-02 16:10:51 +0000 | [diff] [blame] | 708 | llvm::tie(I, E) = Unit.getLocalPreprocessingEntities(); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 709 | |
Argyrios Kyrtzidis | 62288ed | 2012-10-10 02:12:47 +0000 | [diff] [blame] | 710 | bool isModuleFile = Unit.isModuleFile(); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 711 | for (; I != E; ++I) { |
| 712 | PreprocessedEntity *PPE = *I; |
| 713 | |
| 714 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
Argyrios Kyrtzidis | 8d7a24e | 2012-10-18 00:17:05 +0000 | [diff] [blame] | 715 | SourceLocation Loc = ID->getSourceRange().getBegin(); |
| 716 | // Modules have synthetic main files as input, give an invalid location |
| 717 | // if the location points to such a file. |
| 718 | if (isModuleFile && Unit.isInMainFileID(Loc)) |
| 719 | Loc = SourceLocation(); |
| 720 | IdxCtx.ppIncludedFile(Loc, ID->getFileName(), |
| 721 | ID->getFile(), |
| 722 | ID->getKind() == InclusionDirective::Import, |
| 723 | !ID->wasInQuotes(), ID->importedModule()); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
Argyrios Kyrtzidis | 95c579c | 2012-10-03 01:58:28 +0000 | [diff] [blame] | 728 | static bool topLevelDeclVisitor(void *context, const Decl *D) { |
Argyrios Kyrtzidis | 2093e0b | 2012-10-02 21:09:13 +0000 | [diff] [blame] | 729 | IndexingContext &IdxCtx = *static_cast<IndexingContext*>(context); |
| 730 | IdxCtx.indexTopLevelDecl(D); |
| 731 | if (IdxCtx.shouldAbort()) |
| 732 | return false; |
| 733 | return true; |
| 734 | } |
| 735 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 736 | static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) { |
Argyrios Kyrtzidis | 95c579c | 2012-10-03 01:58:28 +0000 | [diff] [blame] | 737 | Unit.visitLocalTopLevelDecls(&IdxCtx, topLevelDeclVisitor); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) { |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 741 | if (!IdxCtx.hasDiagnosticCallback()) |
| 742 | return; |
| 743 | |
| 744 | CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(TU); |
| 745 | IdxCtx.handleDiagnosticSet(DiagSet); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | static void clang_indexTranslationUnit_Impl(void *UserData) { |
| 749 | IndexTranslationUnitInfo *ITUI = |
| 750 | static_cast<IndexTranslationUnitInfo*>(UserData); |
| 751 | CXTranslationUnit TU = ITUI->TU; |
| 752 | CXClientData client_data = ITUI->client_data; |
| 753 | IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks; |
| 754 | unsigned index_callbacks_size = ITUI->index_callbacks_size; |
| 755 | unsigned index_options = ITUI->index_options; |
| 756 | ITUI->result = 1; // init as error. |
| 757 | |
| 758 | if (!TU) |
| 759 | return; |
| 760 | if (!client_index_callbacks || index_callbacks_size == 0) |
| 761 | return; |
| 762 | |
Dmitri Gribenko | 8c718e7 | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 763 | CIndexer *CXXIdx = TU->CIdx; |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 764 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
Argyrios Kyrtzidis | 81b5ac3 | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 765 | setThreadBackgroundPriority(); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 766 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 767 | IndexerCallbacks CB; |
| 768 | memset(&CB, 0, sizeof(CB)); |
| 769 | unsigned ClientCBSize = index_callbacks_size < sizeof(CB) |
| 770 | ? index_callbacks_size : sizeof(CB); |
| 771 | memcpy(&CB, client_index_callbacks, ClientCBSize); |
| 772 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 773 | OwningPtr<IndexingContext> IndexCtx; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 774 | IndexCtx.reset(new IndexingContext(client_data, CB, index_options, TU)); |
| 775 | |
| 776 | // Recover resources if we crash before exiting this method. |
| 777 | llvm::CrashRecoveryContextCleanupRegistrar<IndexingContext> |
| 778 | IndexCtxCleanup(IndexCtx.get()); |
| 779 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 780 | OwningPtr<IndexingConsumer> IndexConsumer; |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 781 | IndexConsumer.reset(new IndexingConsumer(*IndexCtx, 0)); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 782 | |
| 783 | // Recover resources if we crash before exiting this method. |
| 784 | llvm::CrashRecoveryContextCleanupRegistrar<IndexingConsumer> |
| 785 | IndexConsumerCleanup(IndexConsumer.get()); |
| 786 | |
Dmitri Gribenko | 5694feb | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 787 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 788 | if (!Unit) |
| 789 | return; |
| 790 | |
Argyrios Kyrtzidis | 162884d | 2012-09-25 19:29:50 +0000 | [diff] [blame] | 791 | ASTUnit::ConcurrencyCheck Check(*Unit); |
| 792 | |
Argyrios Kyrtzidis | 3da76bf | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 793 | if (const FileEntry *PCHFile = Unit->getPCHFile()) |
| 794 | IndexCtx->importedPCH(PCHFile); |
| 795 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 796 | FileManager &FileMgr = Unit->getFileManager(); |
| 797 | |
| 798 | if (Unit->getOriginalSourceFileName().empty()) |
| 799 | IndexCtx->enteredMainFile(0); |
| 800 | else |
| 801 | IndexCtx->enteredMainFile(FileMgr.getFile(Unit->getOriginalSourceFileName())); |
| 802 | |
| 803 | IndexConsumer->Initialize(Unit->getASTContext()); |
| 804 | |
| 805 | indexPreprocessingRecord(*Unit, *IndexCtx); |
| 806 | indexTranslationUnit(*Unit, *IndexCtx); |
| 807 | indexDiagnostics(TU, *IndexCtx); |
| 808 | |
| 809 | ITUI->result = 0; |
| 810 | } |
| 811 | |
| 812 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 813 | // libclang public APIs. |
| 814 | //===----------------------------------------------------------------------===// |
| 815 | |
| 816 | extern "C" { |
| 817 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 818 | int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) { |
| 819 | return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory; |
| 820 | } |
| 821 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 822 | const CXIdxObjCContainerDeclInfo * |
| 823 | clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) { |
| 824 | if (!DInfo) |
| 825 | return 0; |
| 826 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 827 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 828 | if (const ObjCContainerDeclInfo * |
| 829 | ContInfo = dyn_cast<ObjCContainerDeclInfo>(DI)) |
| 830 | return &ContInfo->ObjCContDeclInfo; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 835 | const CXIdxObjCInterfaceDeclInfo * |
| 836 | clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 837 | if (!DInfo) |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 838 | return 0; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 839 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 840 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 841 | if (const ObjCInterfaceDeclInfo * |
| 842 | InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI)) |
| 843 | return &InterInfo->ObjCInterDeclInfo; |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | const CXIdxObjCCategoryDeclInfo * |
| 849 | clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){ |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 850 | if (!DInfo) |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 851 | return 0; |
| 852 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 853 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 854 | if (const ObjCCategoryDeclInfo * |
| 855 | CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI)) |
| 856 | return &CatInfo->ObjCCatDeclInfo; |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | const CXIdxObjCProtocolRefListInfo * |
| 862 | clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) { |
| 863 | if (!DInfo) |
| 864 | return 0; |
| 865 | |
| 866 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 867 | |
| 868 | if (const ObjCInterfaceDeclInfo * |
| 869 | InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI)) |
| 870 | return InterInfo->ObjCInterDeclInfo.protocols; |
| 871 | |
| 872 | if (const ObjCProtocolDeclInfo * |
| 873 | ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(DI)) |
| 874 | return &ProtInfo->ObjCProtoRefListInfo; |
| 875 | |
Argyrios Kyrtzidis | c10a4c8 | 2011-12-13 18:47:45 +0000 | [diff] [blame] | 876 | if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI)) |
| 877 | return CatInfo->ObjCCatDeclInfo.protocols; |
| 878 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 879 | return 0; |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Argyrios Kyrtzidis | 792db26 | 2012-02-28 17:50:33 +0000 | [diff] [blame] | 882 | const CXIdxObjCPropertyDeclInfo * |
| 883 | clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo *DInfo) { |
| 884 | if (!DInfo) |
| 885 | return 0; |
| 886 | |
| 887 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 888 | if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(DI)) |
| 889 | return &PropInfo->ObjCPropDeclInfo; |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 894 | const CXIdxIBOutletCollectionAttrInfo * |
| 895 | clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) { |
| 896 | if (!AInfo) |
| 897 | return 0; |
| 898 | |
| 899 | const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo); |
| 900 | if (const IBOutletCollectionInfo * |
| 901 | IBInfo = dyn_cast<IBOutletCollectionInfo>(DI)) |
| 902 | return &IBInfo->IBCollInfo; |
| 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 907 | const CXIdxCXXClassDeclInfo * |
| 908 | clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) { |
| 909 | if (!DInfo) |
| 910 | return 0; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 911 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 912 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 913 | if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI)) |
| 914 | return &ClassInfo->CXXClassInfo; |
| 915 | |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | CXIdxClientContainer |
| 920 | clang_index_getClientContainer(const CXIdxContainerInfo *info) { |
| 921 | if (!info) |
| 922 | return 0; |
Benjamin Kramer | 3267e11 | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 923 | const ContainerInfo *Container = static_cast<const ContainerInfo *>(info); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 924 | return Container->IndexCtx->getClientContainerForDC(Container->DC); |
| 925 | } |
| 926 | |
| 927 | void clang_index_setClientContainer(const CXIdxContainerInfo *info, |
| 928 | CXIdxClientContainer client) { |
| 929 | if (!info) |
| 930 | return; |
Benjamin Kramer | 3267e11 | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 931 | const ContainerInfo *Container = static_cast<const ContainerInfo *>(info); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 932 | Container->IndexCtx->addContainerInMap(Container->DC, client); |
| 933 | } |
| 934 | |
| 935 | CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) { |
| 936 | if (!info) |
| 937 | return 0; |
Benjamin Kramer | 3267e11 | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 938 | const EntityInfo *Entity = static_cast<const EntityInfo *>(info); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 939 | return Entity->IndexCtx->getClientEntity(Entity->Dcl); |
| 940 | } |
| 941 | |
| 942 | void clang_index_setClientEntity(const CXIdxEntityInfo *info, |
| 943 | CXIdxClientEntity client) { |
| 944 | if (!info) |
| 945 | return; |
Benjamin Kramer | 3267e11 | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 946 | const EntityInfo *Entity = static_cast<const EntityInfo *>(info); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 947 | Entity->IndexCtx->setClientEntity(Entity->Dcl, client); |
| 948 | } |
| 949 | |
| 950 | CXIndexAction clang_IndexAction_create(CXIndex CIdx) { |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 951 | return new IndexSessionData(CIdx); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | void clang_IndexAction_dispose(CXIndexAction idxAction) { |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 955 | if (idxAction) |
| 956 | delete static_cast<IndexSessionData *>(idxAction); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | int clang_indexSourceFile(CXIndexAction idxAction, |
| 960 | CXClientData client_data, |
| 961 | IndexerCallbacks *index_callbacks, |
| 962 | unsigned index_callbacks_size, |
| 963 | unsigned index_options, |
| 964 | const char *source_filename, |
| 965 | const char * const *command_line_args, |
| 966 | int num_command_line_args, |
| 967 | struct CXUnsavedFile *unsaved_files, |
| 968 | unsigned num_unsaved_files, |
| 969 | CXTranslationUnit *out_TU, |
| 970 | unsigned TU_options) { |
Argyrios Kyrtzidis | c6f5c6a | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 971 | LOG_FUNC_SECTION { |
| 972 | *Log << source_filename << ": "; |
| 973 | for (int i = 0; i != num_command_line_args; ++i) |
| 974 | *Log << command_line_args[i] << " "; |
| 975 | } |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 976 | |
| 977 | IndexSourceFileInfo ITUI = { idxAction, client_data, index_callbacks, |
| 978 | index_callbacks_size, index_options, |
| 979 | source_filename, command_line_args, |
| 980 | num_command_line_args, unsaved_files, |
| 981 | num_unsaved_files, out_TU, TU_options, 0 }; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 982 | |
Argyrios Kyrtzidis | e7de9b4 | 2011-10-29 19:32:39 +0000 | [diff] [blame] | 983 | if (getenv("LIBCLANG_NOTHREADS")) { |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 984 | clang_indexSourceFile_Impl(&ITUI); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 985 | return ITUI.result; |
| 986 | } |
| 987 | |
| 988 | llvm::CrashRecoveryContext CRC; |
| 989 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 990 | if (!RunSafely(CRC, clang_indexSourceFile_Impl, &ITUI)) { |
| 991 | fprintf(stderr, "libclang: crash detected during indexing source file: {\n"); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 992 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 993 | fprintf(stderr, " 'command_line_args' : ["); |
| 994 | for (int i = 0; i != num_command_line_args; ++i) { |
| 995 | if (i) |
| 996 | fprintf(stderr, ", "); |
| 997 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 998 | } |
| 999 | fprintf(stderr, "],\n"); |
| 1000 | fprintf(stderr, " 'unsaved_files' : ["); |
| 1001 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 1002 | if (i) |
| 1003 | fprintf(stderr, ", "); |
| 1004 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 1005 | unsaved_files[i].Length); |
| 1006 | } |
| 1007 | fprintf(stderr, "],\n"); |
| 1008 | fprintf(stderr, " 'options' : %d,\n", TU_options); |
| 1009 | fprintf(stderr, "}\n"); |
| 1010 | |
| 1011 | return 1; |
| 1012 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 1013 | if (out_TU) |
| 1014 | PrintLibclangResourceUsage(*out_TU); |
| 1015 | } |
| 1016 | |
| 1017 | return ITUI.result; |
| 1018 | } |
| 1019 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 1020 | int clang_indexTranslationUnit(CXIndexAction idxAction, |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 1021 | CXClientData client_data, |
| 1022 | IndexerCallbacks *index_callbacks, |
| 1023 | unsigned index_callbacks_size, |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 1024 | unsigned index_options, |
| 1025 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | c6f5c6a | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 1026 | LOG_FUNC_SECTION { |
| 1027 | *Log << TU; |
| 1028 | } |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 1029 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 1030 | IndexTranslationUnitInfo ITUI = { idxAction, client_data, index_callbacks, |
| 1031 | index_callbacks_size, index_options, TU, |
| 1032 | 0 }; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 1033 | |
| 1034 | if (getenv("LIBCLANG_NOTHREADS")) { |
| 1035 | clang_indexTranslationUnit_Impl(&ITUI); |
| 1036 | return ITUI.result; |
| 1037 | } |
| 1038 | |
| 1039 | llvm::CrashRecoveryContext CRC; |
| 1040 | |
| 1041 | if (!RunSafely(CRC, clang_indexTranslationUnit_Impl, &ITUI)) { |
| 1042 | fprintf(stderr, "libclang: crash detected during indexing TU\n"); |
| 1043 | |
| 1044 | return 1; |
| 1045 | } |
| 1046 | |
| 1047 | return ITUI.result; |
| 1048 | } |
| 1049 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1050 | void clang_indexLoc_getFileLocation(CXIdxLoc location, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1051 | CXIdxClientFile *indexFile, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1052 | CXFile *file, |
| 1053 | unsigned *line, |
| 1054 | unsigned *column, |
| 1055 | unsigned *offset) { |
| 1056 | if (indexFile) *indexFile = 0; |
| 1057 | if (file) *file = 0; |
| 1058 | if (line) *line = 0; |
| 1059 | if (column) *column = 0; |
| 1060 | if (offset) *offset = 0; |
| 1061 | |
| 1062 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 1063 | if (!location.ptr_data[0] || Loc.isInvalid()) |
| 1064 | return; |
| 1065 | |
| 1066 | IndexingContext &IndexCtx = |
| 1067 | *static_cast<IndexingContext*>(location.ptr_data[0]); |
| 1068 | IndexCtx.translateLoc(Loc, indexFile, file, line, column, offset); |
| 1069 | } |
| 1070 | |
| 1071 | CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) { |
| 1072 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 1073 | if (!location.ptr_data[0] || Loc.isInvalid()) |
| 1074 | return clang_getNullLocation(); |
| 1075 | |
| 1076 | IndexingContext &IndexCtx = |
| 1077 | *static_cast<IndexingContext*>(location.ptr_data[0]); |
| 1078 | return cxloc::translateSourceLocation(IndexCtx.getASTContext(), Loc); |
| 1079 | } |
| 1080 | |
| 1081 | } // end: extern "C" |
| 1082 | |