ParseAST.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
  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 implements the clang::ParseAST method.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Parse/ParseAST.h"
  13. #include "clang/AST/ASTConsumer.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/ExternalASTSource.h"
  16. #include "clang/AST/Stmt.h"
  17. #include "clang/Parse/ParseDiagnostic.h"
  18. #include "clang/Parse/Parser.h"
  19. #include "clang/Sema/CodeCompleteConsumer.h"
  20. #include "clang/Sema/Sema.h"
  21. #include "clang/Sema/SemaConsumer.h"
  22. #include "clang/Sema/TemplateInstCallback.h"
  23. #include "llvm/Support/CrashRecoveryContext.h"
  24. #include "llvm/Support/TimeProfiler.h"
  25. #include <cstdio>
  26. #include <memory>
  27. using namespace clang;
  28. namespace {
  29. /// Resets LLVM's pretty stack state so that stack traces are printed correctly
  30. /// when there are nested CrashRecoveryContexts and the inner one recovers from
  31. /// a crash.
  32. class ResetStackCleanup
  33. : public llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup,
  34. const void> {
  35. public:
  36. ResetStackCleanup(llvm::CrashRecoveryContext *Context, const void *Top)
  37. : llvm::CrashRecoveryContextCleanupBase<ResetStackCleanup, const void>(
  38. Context, Top) {}
  39. void recoverResources() override {
  40. llvm::RestorePrettyStackState(resource);
  41. }
  42. };
  43. /// If a crash happens while the parser is active, an entry is printed for it.
  44. class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry {
  45. const Parser &P;
  46. public:
  47. PrettyStackTraceParserEntry(const Parser &p) : P(p) {}
  48. void print(raw_ostream &OS) const override;
  49. };
  50. /// If a crash happens while the parser is active, print out a line indicating
  51. /// what the current token is.
  52. void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
  53. const Token &Tok = P.getCurToken();
  54. if (Tok.is(tok::eof)) {
  55. OS << "<eof> parser at end of file\n";
  56. return;
  57. }
  58. if (Tok.getLocation().isInvalid()) {
  59. OS << "<unknown> parser at unknown location\n";
  60. return;
  61. }
  62. const Preprocessor &PP = P.getPreprocessor();
  63. Tok.getLocation().print(OS, PP.getSourceManager());
  64. if (Tok.isAnnotation()) {
  65. OS << ": at annotation token\n";
  66. } else {
  67. // Do the equivalent of PP.getSpelling(Tok) except for the parts that would
  68. // allocate memory.
  69. bool Invalid = false;
  70. const SourceManager &SM = P.getPreprocessor().getSourceManager();
  71. unsigned Length = Tok.getLength();
  72. const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid);
  73. if (Invalid) {
  74. OS << ": unknown current parser token\n";
  75. return;
  76. }
  77. OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n";
  78. }
  79. }
  80. } // namespace
  81. //===----------------------------------------------------------------------===//
  82. // Public interface to the file
  83. //===----------------------------------------------------------------------===//
  84. /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
  85. /// the file is parsed. This inserts the parsed decls into the translation unit
  86. /// held by Ctx.
  87. ///
  88. void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
  89. ASTContext &Ctx, bool PrintStats,
  90. TranslationUnitKind TUKind,
  91. CodeCompleteConsumer *CompletionConsumer,
  92. bool SkipFunctionBodies) {
  93. std::unique_ptr<Sema> S(
  94. new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer));
  95. // Recover resources if we crash before exiting this method.
  96. llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get());
  97. ParseAST(*S.get(), PrintStats, SkipFunctionBodies);
  98. }
  99. void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
  100. // Collect global stats on Decls/Stmts (until we have a module streamer).
  101. if (PrintStats) {
  102. Decl::EnableStatistics();
  103. Stmt::EnableStatistics();
  104. }
  105. // Also turn on collection of stats inside of the Sema object.
  106. bool OldCollectStats = PrintStats;
  107. std::swap(OldCollectStats, S.CollectStats);
  108. // Initialize the template instantiation observer chain.
  109. // FIXME: See note on "finalize" below.
  110. initialize(S.TemplateInstCallbacks, S);
  111. ASTConsumer *Consumer = &S.getASTConsumer();
  112. std::unique_ptr<Parser> ParseOP(
  113. new Parser(S.getPreprocessor(), S, SkipFunctionBodies));
  114. Parser &P = *ParseOP.get();
  115. llvm::CrashRecoveryContextCleanupRegistrar<const void, ResetStackCleanup>
  116. CleanupPrettyStack(llvm::SavePrettyStackState());
  117. PrettyStackTraceParserEntry CrashInfo(P);
  118. // Recover resources if we crash before exiting this method.
  119. llvm::CrashRecoveryContextCleanupRegistrar<Parser>
  120. CleanupParser(ParseOP.get());
  121. S.getPreprocessor().EnterMainSourceFile();
  122. ExternalASTSource *External = S.getASTContext().getExternalSource();
  123. if (External)
  124. External->StartTranslationUnit(Consumer);
  125. // If a PCH through header is specified that does not have an include in
  126. // the source, or a PCH is being created with #pragma hdrstop with nothing
  127. // after the pragma, there won't be any tokens or a Lexer.
  128. bool HaveLexer = S.getPreprocessor().getCurrentLexer();
  129. if (HaveLexer) {
  130. llvm::TimeTraceScope TimeScope("Frontend");
  131. P.Initialize();
  132. Parser::DeclGroupPtrTy ADecl;
  133. Sema::ModuleImportState ImportState;
  134. EnterExpressionEvaluationContext PotentiallyEvaluated(
  135. S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
  136. for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl, ImportState); !AtEOF;
  137. AtEOF = P.ParseTopLevelDecl(ADecl, ImportState)) {
  138. // If we got a null return and something *was* parsed, ignore it. This
  139. // is due to a top-level semicolon, an action override, or a parse error
  140. // skipping something.
  141. if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))
  142. return;
  143. }
  144. }
  145. // Process any TopLevelDecls generated by #pragma weak.
  146. for (Decl *D : S.WeakTopLevelDecls())
  147. Consumer->HandleTopLevelDecl(DeclGroupRef(D));
  148. // For C++20 modules, the codegen for module initializers needs to be altered
  149. // and to be able to use a name based on the module name.
  150. // At this point, we should know if we are building a non-header C++20 module.
  151. if (S.getLangOpts().CPlusPlusModules) {
  152. // If we are building the module from source, then the top level module
  153. // will be here.
  154. Module *CodegenModule = S.getCurrentModule();
  155. bool Interface = true;
  156. if (CodegenModule)
  157. // We only use module initializers for importable module (including
  158. // partition implementation units).
  159. Interface = S.currentModuleIsInterface();
  160. else if (S.getLangOpts().isCompilingModuleInterface())
  161. // If we are building the module from a PCM file, then the module can be
  162. // found here.
  163. CodegenModule = S.getPreprocessor().getCurrentModule();
  164. if (Interface && CodegenModule)
  165. S.getASTContext().setModuleForCodeGen(CodegenModule);
  166. }
  167. Consumer->HandleTranslationUnit(S.getASTContext());
  168. // Finalize the template instantiation observer chain.
  169. // FIXME: This (and init.) should be done in the Sema class, but because
  170. // Sema does not have a reliable "Finalize" function (it has a
  171. // destructor, but it is not guaranteed to be called ("-disable-free")).
  172. // So, do the initialization above and do the finalization here:
  173. finalize(S.TemplateInstCallbacks, S);
  174. std::swap(OldCollectStats, S.CollectStats);
  175. if (PrintStats) {
  176. llvm::errs() << "\nSTATISTICS:\n";
  177. if (HaveLexer) P.getActions().PrintStats();
  178. S.getASTContext().PrintStats();
  179. Decl::PrintStats();
  180. Stmt::PrintStats();
  181. Consumer->PrintStats();
  182. }
  183. }