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" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExternalASTSource.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Stmt.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Parse/ParseDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/Parse/Parser.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 23 | #include "clang/Sema/ExternalSemaSource.h" |
| 24 | #include "clang/Sema/Sema.h" |
| 25 | #include "clang/Sema/SemaConsumer.h" |
Ted Kremenek | 934a571 | 2011-03-18 03:44:21 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/OwningPtr.h" |
Ted Kremenek | 965fe84 | 2011-03-18 02:06:53 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CrashRecoveryContext.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 28 | #include <cstdio> |
| 29 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
Nico Weber | f88836d | 2012-11-27 21:57:34 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | /// If a crash happens while the parser is active, an entry is printed for it. |
| 35 | class PrettyStackTraceParserEntry : public llvm::PrettyStackTraceEntry { |
| 36 | const Parser &P; |
| 37 | public: |
| 38 | PrettyStackTraceParserEntry(const Parser &p) : P(p) {} |
| 39 | virtual void print(raw_ostream &OS) const; |
| 40 | }; |
| 41 | |
| 42 | /// If a crash happens while the parser is active, print out a line indicating |
| 43 | /// what the current token is. |
| 44 | void PrettyStackTraceParserEntry::print(raw_ostream &OS) const { |
| 45 | const Token &Tok = P.getCurToken(); |
| 46 | if (Tok.is(tok::eof)) { |
| 47 | OS << "<eof> parser at end of file\n"; |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | if (Tok.getLocation().isInvalid()) { |
| 52 | OS << "<unknown> parser at unknown location\n"; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | const Preprocessor &PP = P.getPreprocessor(); |
| 57 | Tok.getLocation().print(OS, PP.getSourceManager()); |
Nick Lewycky | cdd81e5 | 2013-03-25 21:24:30 +0000 | [diff] [blame] | 58 | if (Tok.isAnnotation()) { |
| 59 | OS << ": at annotation token\n"; |
| 60 | } else { |
| 61 | // Do the equivalent of PP.getSpelling(Tok) except for the parts that would |
| 62 | // allocate memory. |
| 63 | bool Invalid = false; |
| 64 | const SourceManager &SM = P.getPreprocessor().getSourceManager(); |
| 65 | unsigned Length = Tok.getLength(); |
| 66 | const char *Spelling = SM.getCharacterData(Tok.getLocation(), &Invalid); |
| 67 | if (Invalid) { |
| 68 | OS << ": unknown current parser token\n"; |
| 69 | return; |
| 70 | } |
| 71 | OS << ": current parser token '" << StringRef(Spelling, Length) << "'\n"; |
| 72 | } |
Nico Weber | f88836d | 2012-11-27 21:57:34 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | } // namespace |
| 76 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | //===----------------------------------------------------------------------===// |
| 78 | // Public interface to the file |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 81 | /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 82 | /// the file is parsed. This inserts the parsed decls into the translation unit |
| 83 | /// held by Ctx. |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 84 | /// |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 85 | void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 86 | ASTContext &Ctx, bool PrintStats, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 87 | TranslationUnitKind TUKind, |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 88 | CodeCompleteConsumer *CompletionConsumer, |
| 89 | bool SkipFunctionBodies) { |
Ted Kremenek | 934a571 | 2011-03-18 03:44:21 +0000 | [diff] [blame] | 90 | |
Nico Weber | f88836d | 2012-11-27 21:57:34 +0000 | [diff] [blame] | 91 | OwningPtr<Sema> S(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 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 112 | OwningPtr<Parser> ParseOP(new Parser(S.getPreprocessor(), S, |
| 113 | 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. |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 148 | for (SmallVectorImpl<Decl *>::iterator |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 149 | I = S.WeakTopLevelDecls().begin(), |
| 150 | E = S.WeakTopLevelDecls().end(); I != E; ++I) |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 151 | Consumer->HandleTopLevelDecl(DeclGroupRef(*I)); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 152 | |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 153 | Consumer->HandleTranslationUnit(S.getASTContext()); |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 154 | |
| 155 | std::swap(OldCollectStats, S.CollectStats); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 156 | if (PrintStats) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 157 | llvm::errs() << "\nSTATISTICS:\n"; |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 158 | P.getActions().PrintStats(); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 159 | S.getASTContext().PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 160 | Decl::PrintStats(); |
| 161 | Stmt::PrintStats(); |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 162 | Consumer->PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 163 | } |
| 164 | } |