Coverage Report

Created: 2020-02-25 14:32

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