Coverage Report

Created: 2020-10-24 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/Indexing.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Indexing.cpp - Higher level API functions --------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "CIndexDiagnostic.h"
10
#include "CIndexer.h"
11
#include "CLog.h"
12
#include "CXCursor.h"
13
#include "CXIndexDataConsumer.h"
14
#include "CXSourceLocation.h"
15
#include "CXString.h"
16
#include "CXTranslationUnit.h"
17
#include "clang/AST/ASTConsumer.h"
18
#include "clang/Frontend/ASTUnit.h"
19
#include "clang/Frontend/CompilerInstance.h"
20
#include "clang/Frontend/CompilerInvocation.h"
21
#include "clang/Frontend/FrontendAction.h"
22
#include "clang/Frontend/MultiplexConsumer.h"
23
#include "clang/Frontend/Utils.h"
24
#include "clang/Index/IndexingAction.h"
25
#include "clang/Lex/HeaderSearch.h"
26
#include "clang/Lex/PPCallbacks.h"
27
#include "clang/Lex/PPConditionalDirectiveRecord.h"
28
#include "clang/Lex/Preprocessor.h"
29
#include "clang/Lex/PreprocessorOptions.h"
30
#include "llvm/Support/CrashRecoveryContext.h"
31
#include "llvm/Support/MemoryBuffer.h"
32
#include <cstdio>
33
#include <mutex>
34
#include <utility>
35
36
using namespace clang;
37
using namespace clang::index;
38
using namespace cxtu;
39
using namespace cxindex;
40
41
namespace {
42
43
//===----------------------------------------------------------------------===//
44
// Skip Parsed Bodies
45
//===----------------------------------------------------------------------===//
46
47
/// A "region" in source code identified by the file/offset of the
48
/// preprocessor conditional directive that it belongs to.
49
/// Multiple, non-consecutive ranges can be parts of the same region.
50
///
51
/// As an example of different regions separated by preprocessor directives:
52
///
53
/// \code
54
///   #1
55
/// #ifdef BLAH
56
///   #2
57
/// #ifdef CAKE
58
///   #3
59
/// #endif
60
///   #2
61
/// #endif
62
///   #1
63
/// \endcode
64
///
65
/// There are 3 regions, with non-consecutive parts:
66
///   #1 is identified as the beginning of the file
67
///   #2 is identified as the location of "#ifdef BLAH"
68
///   #3 is identified as the location of "#ifdef CAKE"
69
///
70
class PPRegion {
71
  llvm::sys::fs::UniqueID UniqueID;
72
  time_t ModTime;
73
  unsigned Offset;
74
public:
75
31
  PPRegion() : UniqueID(0, 0), ModTime(), Offset() {}
76
  PPRegion(llvm::sys::fs::UniqueID UniqueID, unsigned offset, time_t modTime)
77
50
      : UniqueID(UniqueID), ModTime(modTime), Offset(offset) {}
78
79
12
  const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
80
12
  unsigned getOffset() const { return Offset; }
81
12
  time_t getModTime() const { return ModTime; }
82
83
15
  bool isInvalid() const { return *this == PPRegion(); }
84
85
278
  friend bool operator==(const PPRegion &lhs, const PPRegion &rhs) {
86
278
    return lhs.UniqueID == rhs.UniqueID && 
lhs.Offset == rhs.Offset207
&&
87
205
           lhs.ModTime == rhs.ModTime;
88
278
  }
89
};
90
91
} // end anonymous namespace
92
93
namespace llvm {
94
95
  template <>
96
  struct DenseMapInfo<PPRegion> {
97
20
    static inline PPRegion getEmptyKey() {
98
20
      return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-1), 0);
99
20
    }
100
15
    static inline PPRegion getTombstoneKey() {
101
15
      return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-2), 0);
102
15
    }
103
104
12
    static unsigned getHashValue(const PPRegion &S) {
105
12
      llvm::FoldingSetNodeID ID;
106
12
      const llvm::sys::fs::UniqueID &UniqueID = S.getUniqueID();
107
12
      ID.AddInteger(UniqueID.getFile());
108
12
      ID.AddInteger(UniqueID.getDevice());
109
12
      ID.AddInteger(S.getOffset());
110
12
      ID.AddInteger(S.getModTime());
111
12
      return ID.ComputeHash();
112
12
    }
113
114
248
    static bool isEqual(const PPRegion &LHS, const PPRegion &RHS) {
115
248
      return LHS == RHS;
116
248
    }
117
  };
118
}
119
120
namespace {
121
122
/// Keeps track of function bodies that have already been parsed.
123
///
124
/// Is thread-safe.
125
class ThreadSafeParsedRegions {
126
  mutable std::mutex Mutex;
127
  llvm::DenseSet<PPRegion> ParsedRegions;
128
129
public:
130
45
  ~ThreadSafeParsedRegions() = default;
131
132
16
  llvm::DenseSet<PPRegion> getParsedRegions() const {
133
16
    std::lock_guard<std::mutex> MG(Mutex);
134
16
    return ParsedRegions;
135
16
  }
136
137
16
  void addParsedRegions(ArrayRef<PPRegion> Regions) {
138
16
    std::lock_guard<std::mutex> MG(Mutex);
139
16
    ParsedRegions.insert(Regions.begin(), Regions.end());
140
16
  }
141
};
142
143
/// Provides information whether source locations have already been parsed in
144
/// another FrontendAction.
145
///
146
/// Is NOT thread-safe.
147
class ParsedSrcLocationsTracker {
148
  ThreadSafeParsedRegions &ParsedRegionsStorage;
149
  PPConditionalDirectiveRecord &PPRec;
150
  Preprocessor &PP;
151
152
  /// Snapshot of the shared state at the point when this instance was
153
  /// constructed.
154
  llvm::DenseSet<PPRegion> ParsedRegionsSnapshot;
155
  /// Regions that were queried during this instance lifetime.
156
  SmallVector<PPRegion, 32> NewParsedRegions;
157
158
  /// Caching the last queried region.
159
  PPRegion LastRegion;
160
  bool LastIsParsed;
161
162
public:
163
  /// Creates snapshot of \p ParsedRegionsStorage.
164
  ParsedSrcLocationsTracker(ThreadSafeParsedRegions &ParsedRegionsStorage,
165
                            PPConditionalDirectiveRecord &ppRec,
166
                            Preprocessor &pp)
167
      : ParsedRegionsStorage(ParsedRegionsStorage), PPRec(ppRec), PP(pp),
168
16
        ParsedRegionsSnapshot(ParsedRegionsStorage.getParsedRegions()) {}
169
170
  /// \returns true iff \p Loc has already been parsed.
171
  ///
172
  /// Can provide false-negative in case the location was parsed after this
173
  /// instance had been constructed.
174
  bool hasAlredyBeenParsed(SourceLocation Loc, FileID FID,
175
15
                           const FileEntry *FE) {
176
15
    assert(FE);
177
15
    PPRegion region = getRegion(Loc, FID, FE);
178
15
    if (region.isInvalid())
179
0
      return false;
180
181
    // Check common case, consecutive functions in the same region.
182
15
    if (LastRegion == region)
183
6
      return LastIsParsed;
184
185
9
    LastRegion = region;
186
    // Source locations can't be revisited during single TU parsing.
187
    // That means if we hit the same region again, it's a different location in
188
    // the same region and so the "is parsed" value from the snapshot is still
189
    // correct.
190
9
    LastIsParsed = ParsedRegionsSnapshot.count(region);
191
9
    if (!LastIsParsed)
192
4
      NewParsedRegions.emplace_back(std::move(region));
193
9
    return LastIsParsed;
194
9
  }
195
196
  /// Updates ParsedRegionsStorage with newly parsed regions.
197
16
  void syncWithStorage() {
198
16
    ParsedRegionsStorage.addParsedRegions(NewParsedRegions);
199
16
  }
200
201
private:
202
15
  PPRegion getRegion(SourceLocation Loc, FileID FID, const FileEntry *FE) {
203
15
    assert(FE);
204
4
    auto Bail = [this, FE]() {
205
4
      if (isParsedOnceInclude(FE)) {
206
4
        const llvm::sys::fs::UniqueID &ID = FE->getUniqueID();
207
4
        return PPRegion(ID, 0, FE->getModificationTime());
208
4
      }
209
0
      return PPRegion();
210
0
    };
211
212
15
    SourceLocation RegionLoc = PPRec.findConditionalDirectiveRegionLoc(Loc);
213
15
    assert(RegionLoc.isFileID());
214
15
    if (RegionLoc.isInvalid())
215
4
      return Bail();
216
217
11
    FileID RegionFID;
218
11
    unsigned RegionOffset;
219
11
    std::tie(RegionFID, RegionOffset) =
220
11
        PPRec.getSourceManager().getDecomposedLoc(RegionLoc);
221
222
11
    if (RegionFID != FID)
223
0
      return Bail();
224
225
11
    const llvm::sys::fs::UniqueID &ID = FE->getUniqueID();
226
11
    return PPRegion(ID, RegionOffset, FE->getModificationTime());
227
11
  }
228
229
4
  bool isParsedOnceInclude(const FileEntry *FE) {
230
4
    return PP.getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE) ||
231
2
           PP.getHeaderSearchInfo().hasFileBeenImported(FE);
232
4
  }
233
};
234
235
//===----------------------------------------------------------------------===//
236
// IndexPPCallbacks
237
//===----------------------------------------------------------------------===//
238
239
class IndexPPCallbacks : public PPCallbacks {
240
  Preprocessor &PP;
241
  CXIndexDataConsumer &DataConsumer;
242
  bool IsMainFileEntered;
243
244
public:
245
  IndexPPCallbacks(Preprocessor &PP, CXIndexDataConsumer &dataConsumer)
246
39
    : PP(PP), DataConsumer(dataConsumer), IsMainFileEntered(false) { }
247
248
  void FileChanged(SourceLocation Loc, FileChangeReason Reason,
249
242
                 SrcMgr::CharacteristicKind FileType, FileID PrevFID) override {
250
242
    if (IsMainFileEntered)
251
203
      return;
252
253
39
    SourceManager &SM = PP.getSourceManager();
254
39
    SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID());
255
256
39
    if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) {
257
39
      IsMainFileEntered = true;
258
39
      DataConsumer.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID()));
259
39
    }
260
39
  }
261
262
  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
263
                          StringRef FileName, bool IsAngled,
264
                          CharSourceRange FilenameRange, const FileEntry *File,
265
                          StringRef SearchPath, StringRef RelativePath,
266
                          const Module *Imported,
267
19
                          SrcMgr::CharacteristicKind FileType) override {
268
19
    bool isImport = (IncludeTok.is(tok::identifier) &&
269
19
            IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
270
19
    DataConsumer.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled,
271
19
                            Imported);
272
19
  }
273
274
  /// MacroDefined - This hook is called whenever a macro definition is seen.
275
13.1k
  void MacroDefined(const Token &Id, const MacroDirective *MD) override {}
276
277
  /// MacroUndefined - This hook is called whenever a macro #undef is seen.
278
  /// MI is released immediately following this callback.
279
  void MacroUndefined(const Token &MacroNameTok,
280
                      const MacroDefinition &MD,
281
0
                      const MacroDirective *UD) override {}
282
283
  /// MacroExpands - This is called by when a macro invocation is found.
284
  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
285
7
                    SourceRange Range, const MacroArgs *Args) override {}
286
287
  /// SourceRangeSkipped - This hook is called when a source range is skipped.
288
  /// \param Range The SourceRange that was skipped. The range begins at the
289
  /// #if/#else directive and ends after the #endif/#else directive.
290
5
  void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override {
291
5
  }
292
};
293
294
//===----------------------------------------------------------------------===//
295
// IndexingConsumer
296
//===----------------------------------------------------------------------===//
297
298
class IndexingConsumer : public ASTConsumer {
299
  CXIndexDataConsumer &DataConsumer;
300
301
public:
302
  IndexingConsumer(CXIndexDataConsumer &dataConsumer,
303
                   ParsedSrcLocationsTracker *parsedLocsTracker)
304
39
      : DataConsumer(dataConsumer) {}
305
306
39
  void Initialize(ASTContext &Context) override {
307
39
    DataConsumer.setASTContext(Context);
308
39
    DataConsumer.startedTranslationUnit();
309
39
  }
310
311
176
  bool HandleTopLevelDecl(DeclGroupRef DG) override {
312
176
    return !DataConsumer.shouldAbort();
313
176
  }
314
};
315
316
//===----------------------------------------------------------------------===//
317
// CaptureDiagnosticConsumer
318
//===----------------------------------------------------------------------===//
319
320
class CaptureDiagnosticConsumer : public DiagnosticConsumer {
321
  SmallVector<StoredDiagnostic, 4> Errors;
322
public:
323
324
  void HandleDiagnostic(DiagnosticsEngine::Level level,
325
4
                        const Diagnostic &Info) override {
326
4
    if (level >= DiagnosticsEngine::Error)
327
1
      Errors.push_back(StoredDiagnostic(level, Info));
328
4
  }
329
};
330
331
//===----------------------------------------------------------------------===//
332
// IndexingFrontendAction
333
//===----------------------------------------------------------------------===//
334
335
class IndexingFrontendAction : public ASTFrontendAction {
336
  std::shared_ptr<CXIndexDataConsumer> DataConsumer;
337
  IndexingOptions Opts;
338
339
  ThreadSafeParsedRegions *SKData;
340
  std::unique_ptr<ParsedSrcLocationsTracker> ParsedLocsTracker;
341
342
public:
343
  IndexingFrontendAction(std::shared_ptr<CXIndexDataConsumer> dataConsumer,
344
                         const IndexingOptions &Opts,
345
                         ThreadSafeParsedRegions *skData)
346
39
      : DataConsumer(std::move(dataConsumer)), Opts(Opts), SKData(skData) {}
347
348
  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
349
39
                                                 StringRef InFile) override {
350
39
    PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
351
352
39
    if (!PPOpts.ImplicitPCHInclude.empty()) {
353
4
      auto File = CI.getFileManager().getFile(PPOpts.ImplicitPCHInclude);
354
4
      if (File)
355
4
        DataConsumer->importedPCH(*File);
356
4
    }
357
358
39
    DataConsumer->setASTContext(CI.getASTContext());
359
39
    Preprocessor &PP = CI.getPreprocessor();
360
39
    PP.addPPCallbacks(std::make_unique<IndexPPCallbacks>(PP, *DataConsumer));
361
39
    DataConsumer->setPreprocessor(CI.getPreprocessorPtr());
362
363
39
    if (SKData) {
364
16
      auto *PPRec = new PPConditionalDirectiveRecord(PP.getSourceManager());
365
16
      PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec));
366
16
      ParsedLocsTracker =
367
16
          std::make_unique<ParsedSrcLocationsTracker>(*SKData, *PPRec, PP);
368
16
    }
369
370
39
    std::vector<std::unique_ptr<ASTConsumer>> Consumers;
371
39
    Consumers.push_back(std::make_unique<IndexingConsumer>(
372
39
        *DataConsumer, ParsedLocsTracker.get()));
373
39
    Consumers.push_back(createIndexingASTConsumer(
374
39
        DataConsumer, Opts, CI.getPreprocessorPtr(),
375
32
        [this](const Decl *D) { return this->shouldSkipFunctionBody(D); }));
376
39
    return std::make_unique<MultiplexConsumer>(std::move(Consumers));
377
39
  }
378
379
32
  bool shouldSkipFunctionBody(const Decl *D) {
380
32
    if (!ParsedLocsTracker) {
381
      // Always skip bodies.
382
0
      return true;
383
0
    }
384
385
32
    const SourceManager &SM = D->getASTContext().getSourceManager();
386
32
    SourceLocation Loc = D->getLocation();
387
32
    if (Loc.isMacroID())
388
0
      return false;
389
32
    if (SM.isInSystemHeader(Loc))
390
0
      return true; // always skip bodies from system headers.
391
392
32
    FileID FID;
393
32
    unsigned Offset;
394
32
    std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
395
    // Don't skip bodies from main files; this may be revisited.
396
32
    if (SM.getMainFileID() == FID)
397
17
      return false;
398
15
    const FileEntry *FE = SM.getFileEntryForID(FID);
399
15
    if (!FE)
400
0
      return false;
401
402
15
    return ParsedLocsTracker->hasAlredyBeenParsed(Loc, FID, FE);
403
15
  }
404
405
117
  TranslationUnitKind getTranslationUnitKind() override {
406
117
    if (DataConsumer->shouldIndexImplicitTemplateInsts())
407
3
      return TU_Complete;
408
114
    else
409
114
      return TU_Prefix;
410
117
  }
411
39
  bool hasCodeCompletionSupport() const override { return false; }
412
413
39
  void EndSourceFileAction() override {
414
39
    if (ParsedLocsTracker)
415
16
      ParsedLocsTracker->syncWithStorage();
416
39
  }
417
};
418
419
//===----------------------------------------------------------------------===//
420
// clang_indexSourceFileUnit Implementation
421
//===----------------------------------------------------------------------===//
422
423
48
static IndexingOptions getIndexingOptionsFromCXOptions(unsigned index_options) {
424
48
  IndexingOptions IdxOpts;
425
48
  if (index_options & CXIndexOpt_IndexFunctionLocalSymbols)
426
3
    IdxOpts.IndexFunctionLocals = true;
427
48
  if (index_options & CXIndexOpt_IndexImplicitTemplateInstantiations)
428
1
    IdxOpts.IndexImplicitInstantiation = true;
429
48
  return IdxOpts;
430
48
}
431
432
struct IndexSessionData {
433
  CXIndex CIdx;
434
  std::unique_ptr<ThreadSafeParsedRegions> SkipBodyData =
435
      std::make_unique<ThreadSafeParsedRegions>();
436
437
45
  explicit IndexSessionData(CXIndex cIdx) : CIdx(cIdx) {}
438
};
439
440
} // anonymous namespace
441
442
static CXErrorCode clang_indexSourceFile_Impl(
443
    CXIndexAction cxIdxAction, CXClientData client_data,
444
    IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size,
445
    unsigned index_options, const char *source_filename,
446
    const char *const *command_line_args, int num_command_line_args,
447
    ArrayRef<CXUnsavedFile> unsaved_files, CXTranslationUnit *out_TU,
448
39
    unsigned TU_options) {
449
39
  if (out_TU)
450
0
    *out_TU = nullptr;
451
39
  bool requestedToGetTU = (out_TU != nullptr);
452
453
39
  if (!cxIdxAction) {
454
0
    return CXError_InvalidArguments;
455
0
  }
456
39
  if (!client_index_callbacks || index_callbacks_size == 0) {
457
0
    return CXError_InvalidArguments;
458
0
  }
459
460
39
  IndexerCallbacks CB;
461
39
  memset(&CB, 0, sizeof(CB));
462
39
  unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
463
39
                                  ? 
index_callbacks_size0
: sizeof(CB);
464
39
  memcpy(&CB, client_index_callbacks, ClientCBSize);
465
466
39
  IndexSessionData *IdxSession = static_cast<IndexSessionData *>(cxIdxAction);
467
39
  CIndexer *CXXIdx = static_cast<CIndexer *>(IdxSession->CIdx);
468
469
39
  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
470
0
    setThreadBackgroundPriority();
471
472
39
  CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All;
473
39
  if (TU_options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles)
474
0
    CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes;
475
39
  if (Logger::isLoggingEnabled())
476
0
    CaptureDiagnostics = CaptureDiagsKind::None;
477
478
39
  CaptureDiagnosticConsumer *CaptureDiag = nullptr;
479
39
  if (CaptureDiagnostics != CaptureDiagsKind::None)
480
39
    CaptureDiag = new CaptureDiagnosticConsumer();
481
482
  // Configure the diagnostics.
483
39
  IntrusiveRefCntPtr<DiagnosticsEngine>
484
39
    Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions,
485
39
                                              CaptureDiag,
486
39
                                              /*ShouldOwnClient=*/true));
487
488
  // Recover resources if we crash before exiting this function.
489
39
  llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
490
39
    llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
491
39
    DiagCleanup(Diags.get());
492
493
39
  std::unique_ptr<std::vector<const char *>> Args(
494
39
      new std::vector<const char *>());
495
496
  // Recover resources if we crash before exiting this method.
497
39
  llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
498
39
    ArgsCleanup(Args.get());
499
  
500
39
  Args->insert(Args->end(), command_line_args,
501
39
               command_line_args + num_command_line_args);
502
503
  // The 'source_filename' argument is optional.  If the caller does not
504
  // specify it then it is assumed that the source file is specified
505
  // in the actual argument list.
506
  // Put the source file after command_line_args otherwise if '-x' flag is
507
  // present it will be unused.
508
39
  if (source_filename)
509
0
    Args->push_back(source_filename);
510
511
39
  std::shared_ptr<CompilerInvocation> CInvok =
512
39
      createInvocationFromCommandLine(*Args, Diags);
513
514
39
  if (!CInvok)
515
0
    return CXError_Failure;
516
517
  // Recover resources if we crash before exiting this function.
518
39
  llvm::CrashRecoveryContextCleanupRegistrar<
519
39
      std::shared_ptr<CompilerInvocation>,
520
39
      llvm::CrashRecoveryContextDestructorCleanup<
521
39
          std::shared_ptr<CompilerInvocation>>>
522
39
      CInvokCleanup(&CInvok);
523
524
39
  if (CInvok->getFrontendOpts().Inputs.empty())
525
0
    return CXError_Failure;
526
527
39
  typedef SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 8> MemBufferOwner;
528
39
  std::unique_ptr<MemBufferOwner> BufOwner(new MemBufferOwner);
529
530
  // Recover resources if we crash before exiting this method.
531
39
  llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> BufOwnerCleanup(
532
39
      BufOwner.get());
533
534
0
  for (auto &UF : unsaved_files) {
535
0
    std::unique_ptr<llvm::MemoryBuffer> MB =
536
0
        llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
537
0
    CInvok->getPreprocessorOpts().addRemappedFile(UF.Filename, MB.get());
538
0
    BufOwner->push_back(std::move(MB));
539
0
  }
540
541
  // Since libclang is primarily used by batch tools dealing with
542
  // (often very broken) source code, where spell-checking can have a
543
  // significant negative impact on performance (particularly when 
544
  // precompiled headers are involved), we disable it.
545
39
  CInvok->getLangOpts()->SpellChecking = false;
546
547
39
  if (index_options & CXIndexOpt_SuppressWarnings)
548
0
    CInvok->getDiagnosticOpts().IgnoreWarnings = true;
549
550
  // Make sure to use the raw module format.
551
39
  CInvok->getHeaderSearchOpts().ModuleFormat = std::string(
552
39
      CXXIdx->getPCHContainerOperations()->getRawReader().getFormat());
553
554
39
  auto Unit = ASTUnit::create(CInvok, Diags, CaptureDiagnostics,
555
39
                              /*UserFilesAreVolatile=*/true);
556
39
  if (!Unit)
557
0
    return CXError_InvalidArguments;
558
559
39
  auto *UPtr = Unit.get();
560
39
  std::unique_ptr<CXTUOwner> CXTU(
561
39
      new CXTUOwner(MakeCXTranslationUnit(CXXIdx, std::move(Unit))));
562
563
  // Recover resources if we crash before exiting this method.
564
39
  llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
565
39
    CXTUCleanup(CXTU.get());
566
567
  // Enable the skip-parsed-bodies optimization only for C++; this may be
568
  // revisited.
569
39
  bool SkipBodies = (index_options & CXIndexOpt_SkipParsedBodiesInSession) &&
570
39
      CInvok->getLangOpts()->CPlusPlus;
571
39
  if (SkipBodies)
572
16
    CInvok->getFrontendOpts().SkipFunctionBodies = true;
573
574
39
  auto DataConsumer =
575
39
    std::make_shared<CXIndexDataConsumer>(client_data, CB, index_options,
576
39
                                          CXTU->getTU());
577
39
  auto IndexAction = std::make_unique<IndexingFrontendAction>(
578
39
      DataConsumer, getIndexingOptionsFromCXOptions(index_options),
579
23
      SkipBodies ? 
IdxSession->SkipBodyData.get()16
: nullptr);
580
581
  // Recover resources if we crash before exiting this method.
582
39
  llvm::CrashRecoveryContextCleanupRegistrar<FrontendAction>
583
39
    IndexActionCleanup(IndexAction.get());
584
585
39
  bool Persistent = requestedToGetTU;
586
39
  bool OnlyLocalDecls = false;
587
39
  bool PrecompilePreamble = false;
588
39
  bool CreatePreambleOnFirstParse = false;
589
39
  bool CacheCodeCompletionResults = false;
590
39
  PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts(); 
591
39
  PPOpts.AllowPCHWithCompilerErrors = true;
592
593
39
  if (requestedToGetTU) {
594
0
    OnlyLocalDecls = CXXIdx->getOnlyLocalDecls();
595
0
    PrecompilePreamble = TU_options & CXTranslationUnit_PrecompiledPreamble;
596
0
    CreatePreambleOnFirstParse =
597
0
        TU_options & CXTranslationUnit_CreatePreambleOnFirstParse;
598
    // FIXME: Add a flag for modules.
599
0
    CacheCodeCompletionResults
600
0
      = TU_options & CXTranslationUnit_CacheCompletionResults;
601
0
  }
602
603
39
  if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) {
604
39
    PPOpts.DetailedRecord = true;
605
39
  }
606
607
39
  if (!requestedToGetTU && !CInvok->getLangOpts()->Modules)
608
31
    PPOpts.DetailedRecord = false;
609
610
  // Unless the user specified that they want the preamble on the first parse
611
  // set it up to be created on the first reparse. This makes the first parse
612
  // faster, trading for a slower (first) reparse.
613
39
  unsigned PrecompilePreambleAfterNParses =
614
39
      !PrecompilePreamble ? 0 : 
2 - CreatePreambleOnFirstParse0
;
615
39
  DiagnosticErrorTrap DiagTrap(*Diags);
616
39
  bool Success = ASTUnit::LoadFromCompilerInvocationAction(
617
39
      std::move(CInvok), CXXIdx->getPCHContainerOperations(), Diags,
618
39
      IndexAction.get(), UPtr, Persistent, CXXIdx->getClangResourcesPath(),
619
39
      OnlyLocalDecls, CaptureDiagnostics, PrecompilePreambleAfterNParses,
620
39
      CacheCodeCompletionResults,
621
39
      /*IncludeBriefCommentsInCodeCompletion=*/false,
622
39
      /*UserFilesAreVolatile=*/true);
623
39
  if (DiagTrap.hasErrorOccurred() && 
CXXIdx->getDisplayDiagnostics()6
)
624
6
    printDiagsToStderr(UPtr);
625
626
39
  if (isASTReadError(UPtr))
627
0
    return CXError_ASTReadError;
628
629
39
  if (!Success)
630
0
    return CXError_Failure;
631
632
39
  if (out_TU)
633
0
    *out_TU = CXTU->takeTU();
634
635
39
  return CXError_Success;
636
39
}
637
638
//===----------------------------------------------------------------------===//
639
// clang_indexTranslationUnit Implementation
640
//===----------------------------------------------------------------------===//
641
642
9
static void indexPreprocessingRecord(ASTUnit &Unit, CXIndexDataConsumer &IdxCtx) {
643
9
  Preprocessor &PP = Unit.getPreprocessor();
644
9
  if (!PP.getPreprocessingRecord())
645
0
    return;
646
647
  // FIXME: Only deserialize inclusion directives.
648
649
9
  bool isModuleFile = Unit.isModuleFile();
650
3.32k
  for (PreprocessedEntity *PPE : Unit.getLocalPreprocessingEntities()) {
651
3.32k
    if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
652
26
      SourceLocation Loc = ID->getSourceRange().getBegin();
653
      // Modules have synthetic main files as input, give an invalid location
654
      // if the location points to such a file.
655
26
      if (isModuleFile && 
Unit.isInMainFileID(Loc)25
)
656
18
        Loc = SourceLocation();
657
26
      IdxCtx.ppIncludedFile(Loc, ID->getFileName(),
658
26
                            ID->getFile(),
659
26
                            ID->getKind() == InclusionDirective::Import,
660
26
                            !ID->wasInQuotes(), ID->importedModule());
661
26
    }
662
3.32k
  }
663
9
}
664
665
static CXErrorCode clang_indexTranslationUnit_Impl(
666
    CXIndexAction idxAction, CXClientData client_data,
667
    IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size,
668
9
    unsigned index_options, CXTranslationUnit TU) {
669
  // Check arguments.
670
9
  if (isNotUsableTU(TU)) {
671
0
    LOG_BAD_TU(TU);
672
0
    return CXError_InvalidArguments;
673
0
  }
674
9
  if (!client_index_callbacks || index_callbacks_size == 0) {
675
0
    return CXError_InvalidArguments;
676
0
  }
677
678
9
  CIndexer *CXXIdx = TU->CIdx;
679
9
  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
680
0
    setThreadBackgroundPriority();
681
682
9
  IndexerCallbacks CB;
683
9
  memset(&CB, 0, sizeof(CB));
684
9
  unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
685
9
                                  ? 
index_callbacks_size0
: sizeof(CB);
686
9
  memcpy(&CB, client_index_callbacks, ClientCBSize);
687
688
9
  CXIndexDataConsumer DataConsumer(client_data, CB, index_options, TU);
689
690
9
  ASTUnit *Unit = cxtu::getASTUnit(TU);
691
9
  if (!Unit)
692
0
    return CXError_Failure;
693
694
9
  ASTUnit::ConcurrencyCheck Check(*Unit);
695
696
9
  if (const FileEntry *PCHFile = Unit->getPCHFile())
697
0
    DataConsumer.importedPCH(PCHFile);
698
699
9
  FileManager &FileMgr = Unit->getFileManager();
700
701
9
  if (Unit->getOriginalSourceFileName().empty())
702
6
    DataConsumer.enteredMainFile(nullptr);
703
3
  else if (auto MainFile = FileMgr.getFile(Unit->getOriginalSourceFileName()))
704
3
    DataConsumer.enteredMainFile(*MainFile);
705
0
  else
706
0
    DataConsumer.enteredMainFile(nullptr);
707
708
9
  DataConsumer.setASTContext(Unit->getASTContext());
709
9
  DataConsumer.startedTranslationUnit();
710
711
9
  indexPreprocessingRecord(*Unit, DataConsumer);
712
9
  indexASTUnit(*Unit, DataConsumer, getIndexingOptionsFromCXOptions(index_options));
713
9
  DataConsumer.indexDiagnostics();
714
715
9
  return CXError_Success;
716
9
}
717
718
//===----------------------------------------------------------------------===//
719
// libclang public APIs.
720
//===----------------------------------------------------------------------===//
721
722
316
int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) {
723
316
  return CXIdxEntity_ObjCClass <= K && 
K <= CXIdxEntity_ObjCCategory187
;
724
316
}
725
726
const CXIdxObjCContainerDeclInfo *
727
40
clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) {
728
40
  if (!DInfo)
729
0
    return nullptr;
730
731
40
  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
732
40
  if (const ObjCContainerDeclInfo *
733
40
        ContInfo = dyn_cast<ObjCContainerDeclInfo>(DI))
734
40
    return &ContInfo->ObjCContDeclInfo;
735
736
0
  return nullptr;
737
0
}
738
739
const CXIdxObjCInterfaceDeclInfo *
740
316
clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) {
741
316
  if (!DInfo)
742
0
    return nullptr;
743
744
316
  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
745
316
  if (const ObjCInterfaceDeclInfo *
746
316
        InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
747
21
    return &InterInfo->ObjCInterDeclInfo;
748
749
295
  return nullptr;
750
295
}
751
752
const CXIdxObjCCategoryDeclInfo *
753
316
clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){
754
316
  if (!DInfo)
755
0
    return nullptr;
756
757
316
  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
758
316
  if (const ObjCCategoryDeclInfo *
759
316
        CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
760
5
    return &CatInfo->ObjCCatDeclInfo;
761
762
311
  return nullptr;
763
311
}
764
765
const CXIdxObjCProtocolRefListInfo *
766
316
clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) {
767
316
  if (!DInfo)
768
0
    return nullptr;
769
770
316
  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
771
  
772
316
  if (const ObjCInterfaceDeclInfo *
773
316
        InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
774
21
    return InterInfo->ObjCInterDeclInfo.protocols;
775
  
776
295
  if (const ObjCProtocolDeclInfo *
777
295
        ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(DI))
778
4
    return &ProtInfo->ObjCProtoRefListInfo;
779
780
291
  if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
781
5
    return CatInfo->ObjCCatDeclInfo.protocols;
782
783
286
  return nullptr;
784
286
}
785
786
const CXIdxObjCPropertyDeclInfo *
787
316
clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo *DInfo) {
788
316
  if (!DInfo)
789
0
    return nullptr;
790
791
316
  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
792
316
  if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(DI))
793
8
    return &PropInfo->ObjCPropDeclInfo;
794
795
308
  return nullptr;
796
308
}
797
798
const CXIdxIBOutletCollectionAttrInfo *
799
0
clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) {
800
0
  if (!AInfo)
801
0
    return nullptr;
802
803
0
  const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo);
804
0
  if (const IBOutletCollectionInfo *
805
0
        IBInfo = dyn_cast<IBOutletCollectionInfo>(DI))
806
0
    return &IBInfo->IBCollInfo;
807
808
0
  return nullptr;
809
0
}
810
811
const CXIdxCXXClassDeclInfo *
812
316
clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) {
813
316
  if (!DInfo)
814
0
    return nullptr;
815
816
316
  const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
817
316
  if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI))
818
34
    return &ClassInfo->CXXClassInfo;
819
820
282
  return nullptr;
821
282
}
822
823
CXIdxClientContainer
824
2.53k
clang_index_getClientContainer(const CXIdxContainerInfo *info) {
825
2.53k
  if (!info)
826
0
    return nullptr;
827
2.53k
  const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
828
2.53k
  return Container->IndexCtx->getClientContainerForDC(Container->DC);
829
2.53k
}
830
831
void clang_index_setClientContainer(const CXIdxContainerInfo *info,
832
125
                                    CXIdxClientContainer client) {
833
125
  if (!info)
834
0
    return;
835
125
  const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
836
125
  Container->IndexCtx->addContainerInMap(Container->DC, client);
837
125
}
838
839
0
CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) {
840
0
  if (!info)
841
0
    return nullptr;
842
0
  const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
843
0
  return Entity->IndexCtx->getClientEntity(Entity->Dcl);
844
0
}
845
846
void clang_index_setClientEntity(const CXIdxEntityInfo *info,
847
0
                                 CXIdxClientEntity client) {
848
0
  if (!info)
849
0
    return;
850
0
  const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
851
0
  Entity->IndexCtx->setClientEntity(Entity->Dcl, client);
852
0
}
853
854
45
CXIndexAction clang_IndexAction_create(CXIndex CIdx) {
855
45
  return new IndexSessionData(CIdx);
856
45
}
857
858
45
void clang_IndexAction_dispose(CXIndexAction idxAction) {
859
45
  if (idxAction)
860
45
    delete static_cast<IndexSessionData *>(idxAction);
861
45
}
862
863
int clang_indexSourceFile(CXIndexAction idxAction,
864
                          CXClientData client_data,
865
                          IndexerCallbacks *index_callbacks,
866
                          unsigned index_callbacks_size,
867
                          unsigned index_options,
868
                          const char *source_filename,
869
                          const char * const *command_line_args,
870
                          int num_command_line_args,
871
                          struct CXUnsavedFile *unsaved_files,
872
                          unsigned num_unsaved_files,
873
                          CXTranslationUnit *out_TU,
874
39
                          unsigned TU_options) {
875
39
  SmallVector<const char *, 4> Args;
876
39
  Args.push_back("clang");
877
39
  Args.append(command_line_args, command_line_args + num_command_line_args);
878
39
  return clang_indexSourceFileFullArgv(
879
39
      idxAction, client_data, index_callbacks, index_callbacks_size,
880
39
      index_options, source_filename, Args.data(), Args.size(), unsaved_files,
881
39
      num_unsaved_files, out_TU, TU_options);
882
39
}
883
884
int clang_indexSourceFileFullArgv(
885
    CXIndexAction idxAction, CXClientData client_data,
886
    IndexerCallbacks *index_callbacks, unsigned index_callbacks_size,
887
    unsigned index_options, const char *source_filename,
888
    const char *const *command_line_args, int num_command_line_args,
889
    struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files,
890
39
    CXTranslationUnit *out_TU, unsigned TU_options) {
891
39
  LOG_FUNC_SECTION {
892
0
    *Log << source_filename << ": ";
893
0
    for (int i = 0; i != num_command_line_args; ++i)
894
0
      *Log << command_line_args[i] << " ";
895
0
  }
896
897
39
  if (num_unsaved_files && 
!unsaved_files0
)
898
0
    return CXError_InvalidArguments;
899
900
39
  CXErrorCode result = CXError_Failure;
901
39
  auto IndexSourceFileImpl = [=, &result]() {
902
39
    result = clang_indexSourceFile_Impl(
903
39
        idxAction, client_data, index_callbacks, index_callbacks_size,
904
39
        index_options, source_filename, command_line_args,
905
39
        num_command_line_args,
906
39
        llvm::makeArrayRef(unsaved_files, num_unsaved_files), out_TU,
907
39
        TU_options);
908
39
  };
909
910
39
  llvm::CrashRecoveryContext CRC;
911
912
39
  if (!RunSafely(CRC, IndexSourceFileImpl)) {
913
0
    fprintf(stderr, "libclang: crash detected during indexing source file: {\n");
914
0
    fprintf(stderr, "  'source_filename' : '%s'\n", source_filename);
915
0
    fprintf(stderr, "  'command_line_args' : [");
916
0
    for (int i = 0; i != num_command_line_args; ++i) {
917
0
      if (i)
918
0
        fprintf(stderr, ", ");
919
0
      fprintf(stderr, "'%s'", command_line_args[i]);
920
0
    }
921
0
    fprintf(stderr, "],\n");
922
0
    fprintf(stderr, "  'unsaved_files' : [");
923
0
    for (unsigned i = 0; i != num_unsaved_files; ++i) {
924
0
      if (i)
925
0
        fprintf(stderr, ", ");
926
0
      fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
927
0
              unsaved_files[i].Length);
928
0
    }
929
0
    fprintf(stderr, "],\n");
930
0
    fprintf(stderr, "  'options' : %d,\n", TU_options);
931
0
    fprintf(stderr, "}\n");
932
    
933
0
    return 1;
934
39
  } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
935
0
    if (out_TU)
936
0
      PrintLibclangResourceUsage(*out_TU);
937
0
  }
938
939
39
  return result;
940
39
}
941
942
int clang_indexTranslationUnit(CXIndexAction idxAction,
943
                               CXClientData client_data,
944
                               IndexerCallbacks *index_callbacks,
945
                               unsigned index_callbacks_size,
946
                               unsigned index_options,
947
9
                               CXTranslationUnit TU) {
948
9
  LOG_FUNC_SECTION {
949
0
    *Log << TU;
950
0
  }
951
952
9
  CXErrorCode result;
953
9
  auto IndexTranslationUnitImpl = [=, &result]() {
954
9
    result = clang_indexTranslationUnit_Impl(
955
9
        idxAction, client_data, index_callbacks, index_callbacks_size,
956
9
        index_options, TU);
957
9
  };
958
959
9
  llvm::CrashRecoveryContext CRC;
960
961
9
  if (!RunSafely(CRC, IndexTranslationUnitImpl)) {
962
0
    fprintf(stderr, "libclang: crash detected during indexing TU\n");
963
    
964
0
    return 1;
965
0
  }
966
967
9
  return result;
968
9
}
969
970
void clang_indexLoc_getFileLocation(CXIdxLoc location,
971
                                    CXIdxClientFile *indexFile,
972
                                    CXFile *file,
973
                                    unsigned *line,
974
                                    unsigned *column,
975
2.42k
                                    unsigned *offset) {
976
2.42k
  if (indexFile) *indexFile = nullptr;
977
2.42k
  if (file)   
*file = nullptr0
;
978
2.42k
  if (line)   *line = 0;
979
2.42k
  if (column) *column = 0;
980
2.42k
  if (offset) 
*offset = 00
;
981
982
2.42k
  SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
983
2.42k
  if (!location.ptr_data[0] || 
Loc.isInvalid()2.40k
)
984
18
    return;
985
986
2.40k
  CXIndexDataConsumer &DataConsumer =
987
2.40k
      *static_cast<CXIndexDataConsumer*>(location.ptr_data[0]);
988
2.40k
  DataConsumer.translateLoc(Loc, indexFile, file, line, column, offset);
989
2.40k
}
990
991
0
CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) {
992
0
  SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
993
0
  if (!location.ptr_data[0] || Loc.isInvalid())
994
0
    return clang_getNullLocation();
995
996
0
  CXIndexDataConsumer &DataConsumer =
997
0
      *static_cast<CXIndexDataConsumer*>(location.ptr_data[0]);
998
0
  return cxloc::translateSourceLocation(DataConsumer.getASTContext(), Loc);
999
0
}