CIndexInclusionStack.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===- CIndexInclusionStack.cpp - Clang-C Source Indexing Library ---------===//
  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. // This file defines a callback mechanism for clients to get the inclusion
  10. // stack from a translation unit.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CIndexer.h"
  14. #include "CXSourceLocation.h"
  15. #include "CXTranslationUnit.h"
  16. #include "clang/AST/DeclVisitor.h"
  17. #include "clang/Frontend/ASTUnit.h"
  18. using namespace clang;
  19. namespace {
  20. void getInclusions(bool IsLocal, unsigned n, CXTranslationUnit TU,
  21. CXInclusionVisitor CB, CXClientData clientData) {
  22. ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
  23. SourceManager &SM = CXXUnit->getSourceManager();
  24. ASTContext &Ctx = CXXUnit->getASTContext();
  25. SmallVector<CXSourceLocation, 10> InclusionStack;
  26. const bool HasPreamble = SM.getPreambleFileID().isValid();
  27. for (unsigned i = 0 ; i < n ; ++i) {
  28. bool Invalid = false;
  29. const SrcMgr::SLocEntry &SL =
  30. IsLocal ? SM.getLocalSLocEntry(i) : SM.getLoadedSLocEntry(i, &Invalid);
  31. if (!SL.isFile() || Invalid)
  32. continue;
  33. const SrcMgr::FileInfo &FI = SL.getFile();
  34. if (!FI.getContentCache().OrigEntry)
  35. continue;
  36. // If this is the main file, and there is a preamble, skip this SLoc. The
  37. // inclusions of the preamble already showed it.
  38. SourceLocation L = FI.getIncludeLoc();
  39. if (HasPreamble && CXXUnit->isInMainFileID(L))
  40. continue;
  41. // Build the inclusion stack.
  42. InclusionStack.clear();
  43. while (L.isValid()) {
  44. PresumedLoc PLoc = SM.getPresumedLoc(L);
  45. InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L));
  46. L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation();
  47. }
  48. // If there is a preamble, the last entry is the "inclusion" of that
  49. // preamble into the main file, which has the bogus entry of main.c:1:1
  50. if (HasPreamble && !InclusionStack.empty())
  51. InclusionStack.pop_back();
  52. // Callback to the client.
  53. // FIXME: We should have a function to construct CXFiles.
  54. CB(static_cast<CXFile>(const_cast<FileEntry *>(
  55. static_cast<const FileEntry *>(FI.getContentCache().OrigEntry))),
  56. InclusionStack.data(), InclusionStack.size(), clientData);
  57. }
  58. }
  59. } // namespace
  60. void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
  61. CXClientData clientData) {
  62. if (cxtu::isNotUsableTU(TU)) {
  63. LOG_BAD_TU(TU);
  64. return;
  65. }
  66. SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
  67. const unsigned n = SM.local_sloc_entry_size();
  68. // In the case where all the SLocEntries are in an external source, traverse
  69. // those SLocEntries as well. This is the case where we are looking
  70. // at the inclusion stack of an AST/PCH file. Also, if we are not looking at
  71. // a AST/PCH file, but this file has a pre-compiled preamble, we also need
  72. // to look in that file.
  73. if (n == 1 || SM.getPreambleFileID().isValid()) {
  74. getInclusions(/*IsLocal=*/false, SM.loaded_sloc_entry_size(), TU, CB,
  75. clientData);
  76. }
  77. // Not a PCH/AST file. Note, if there is a preamble, it could still be that
  78. // there are #includes in this file (e.g. for any include after the first
  79. // declaration).
  80. if (n != 1)
  81. getInclusions(/*IsLocal=*/true, n, TU, CB, clientData);
  82. }