Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 1 | //===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 10 | // This file implements the clang::ParseAST method. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Parse/ParseAST.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Benjamin Kramer | 478851c | 2012-07-04 17:04:04 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExternalASTSource.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/Stmt.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Parse/ParseDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/Parse/Parser.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 22 | #include "clang/Sema/ExternalSemaSource.h" |
| 23 | #include "clang/Sema/Sema.h" |
| 24 | #include "clang/Sema/SemaConsumer.h" |
Ted Kremenek | 965fe84 | 2011-03-18 02:06:53 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CrashRecoveryContext.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 26 | #include <cstdio> |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 27 | #include <memory> |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 28 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Nico Weber | f88836d | 2012-11-27 21:57:34 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | /// If a crash happens while the parser is active, an entry is printed for it. |
| 34 | class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry { |
| 35 | const Parser &P; |
| 36 | public: |
| 37 | PrettyStackTraceParserEntry(const Parser &p) : P(p) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 38 | void print(raw_ostream &OS) const override; |
Nico Weber | f88836d | 2012-11-27 21:57:34 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /// If a crash happens while the parser is active, print out a line indicating |
| 42 | /// what the current token is. |
| 43 | void PrettyStackTraceParserEntry::print(raw_ostream &OS) const { |
| 44 | const Token &Tok = P.getCurToken(); |
| 45 | if (Tok.is(tok::eof)) { |
| 46 | OS << "<eof> parser at end of file\n"; |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | if (Tok.getLocation().isInvalid()) { |
| 51 | OS << "<unknown> parser at unknown location\n"; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | const Preprocessor &PP = P.getPreprocessor(); |
| 56 | Tok.getLocation().print(OS, PP.getSourceManager()); |
Nick Lewycky | cdd81e5 | 2013-03-25 21:24:30 +0000 | [diff] [blame] | 57 | if (Tok.isAnnotation()) { |
| 58 | OS << ": at annotation token\n"; |
| 59 | } else { |
| 60 | // Do the equivalent of PP.getSpelling(Tok) except for the parts that would |
| 61 | // allocate memory. |
| 62 | bool Invalid = false; |
| 63 | const SourceManager &SM = P.getPreprocessor().getSourceManager(); |
| 64 | unsigned Length = Tok.getLength(); |
| 65 | const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid); |
| 66 | if (Invalid) { |
| 67 | OS << ": unknown current parser token\n"; |
| 68 | return; |
| 69 | } |
| 70 | OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n"; |
| 71 | } |
Nico Weber | f88836d | 2012-11-27 21:57:34 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | } // namespace |
| 75 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | //===----------------------------------------------------------------------===// |
| 77 | // Public interface to the file |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 80 | /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 81 | /// the file is parsed. This inserts the parsed decls into the translation unit |
| 82 | /// held by Ctx. |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 83 | /// |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 84 | void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 85 | ASTContext &Ctx, bool PrintStats, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 86 | TranslationUnitKind TUKind, |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 87 | CodeCompleteConsumer *CompletionConsumer, |
| 88 | bool SkipFunctionBodies) { |
Ted Kremenek | 934a571 | 2011-03-18 03:44:21 +0000 | [diff] [blame] | 89 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 90 | std::unique_ptr<Sema> S( |
| 91 | new Sema(PP, Ctx, *Consumer, TUKind, CompletionConsumer)); |
Ted Kremenek | 965fe84 | 2011-03-18 02:06:53 +0000 | [diff] [blame] | 92 | |
| 93 | // Recover resources if we crash before exiting this method. |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 94 | llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get()); |
Ted Kremenek | 965fe84 | 2011-03-18 02:06:53 +0000 | [diff] [blame] | 95 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 96 | ParseAST(*S.get(), PrintStats, SkipFunctionBodies); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 99 | void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) { |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 100 | // Collect global stats on Decls/Stmts (until we have a module streamer). |
| 101 | if (PrintStats) { |
Daniel Dunbar | 02892a6 | 2012-03-05 21:42:49 +0000 | [diff] [blame] | 102 | Decl::EnableStatistics(); |
| 103 | Stmt::EnableStatistics(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 104 | } |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 105 | |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 106 | // Also turn on collection of stats inside of the Sema object. |
| 107 | bool OldCollectStats = PrintStats; |
| 108 | std::swap(OldCollectStats, S.CollectStats); |
| 109 | |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 110 | ASTConsumer *Consumer = &S.getASTConsumer(); |
John McCall | e783d00 | 2010-08-12 21:23:27 +0000 | [diff] [blame] | 111 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 112 | std::unique_ptr<Parser> ParseOP( |
| 113 | new Parser(S.getPreprocessor(), S, SkipFunctionBodies)); |
Ted Kremenek | 614f96a | 2011-03-22 01:15:17 +0000 | [diff] [blame] | 114 | Parser &P = *ParseOP.get(); |
| 115 | |
| 116 | PrettyStackTraceParserEntry CrashInfo(P); |
| 117 | |
| 118 | // Recover resources if we crash before exiting this method. |
| 119 | llvm::CrashRecoveryContextCleanupRegistrar<Parser> |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 120 | CleanupParser(ParseOP.get()); |
Ted Kremenek | 614f96a | 2011-03-22 01:15:17 +0000 | [diff] [blame] | 121 | |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 122 | S.getPreprocessor().EnterMainSourceFile(); |
John McCall | d69fd7f | 2010-08-12 21:39:05 +0000 | [diff] [blame] | 123 | P.Initialize(); |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame] | 124 | |
| 125 | // C11 6.9p1 says translation units must have at least one top-level |
| 126 | // declaration. C++ doesn't have this restriction. We also don't want to |
| 127 | // complain if we have a precompiled header, although technically if the PCH |
| 128 | // is empty we should still emit the (pedantic) diagnostic. |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 129 | Parser::DeclGroupPtrTy ADecl; |
Meador Inge | 9416d42 | 2012-06-19 18:17:30 +0000 | [diff] [blame] | 130 | ExternalASTSource *External = S.getASTContext().getExternalSource(); |
| 131 | if (External) |
| 132 | External->StartTranslationUnit(Consumer); |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 133 | |
Meador Inge | 9416d42 | 2012-06-19 18:17:30 +0000 | [diff] [blame] | 134 | if (P.ParseTopLevelDecl(ADecl)) { |
| 135 | if (!External && !S.getLangOpts().CPlusPlus) |
| 136 | P.Diag(diag::ext_empty_translation_unit); |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame] | 137 | } else { |
Meador Inge | 9416d42 | 2012-06-19 18:17:30 +0000 | [diff] [blame] | 138 | do { |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame] | 139 | // If we got a null return and something *was* parsed, ignore it. This |
| 140 | // is due to a top-level semicolon, an action override, or a parse error |
| 141 | // skipping something. |
Meador Inge | 9416d42 | 2012-06-19 18:17:30 +0000 | [diff] [blame] | 142 | if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get())) |
Chad Rosier | eb4c01e | 2012-12-21 19:43:33 +0000 | [diff] [blame] | 143 | return; |
Meador Inge | 9416d42 | 2012-06-19 18:17:30 +0000 | [diff] [blame] | 144 | } while (!P.ParseTopLevelDecl(ADecl)); |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame] | 145 | } |
Meador Inge | 9416d42 | 2012-06-19 18:17:30 +0000 | [diff] [blame] | 146 | |
Daniel Dunbar | ec2a4ed | 2009-12-01 21:57:20 +0000 | [diff] [blame] | 147 | // Process any TopLevelDecls generated by #pragma weak. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 148 | for (Decl *D : S.WeakTopLevelDecls()) |
| 149 | Consumer->HandleTopLevelDecl(DeclGroupRef(D)); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 150 | |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 151 | Consumer->HandleTranslationUnit(S.getASTContext()); |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 152 | |
| 153 | std::swap(OldCollectStats, S.CollectStats); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 154 | if (PrintStats) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 155 | llvm::errs() << "\nSTATISTICS:\n"; |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 156 | P.getActions().PrintStats(); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 157 | S.getASTContext().PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 158 | Decl::PrintStats(); |
| 159 | Stmt::PrintStats(); |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 160 | Consumer->PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 161 | } |
| 162 | } |