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" |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame^] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Sema.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 18 | #include "clang/Sema/SemaConsumer.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ExternalSemaSource.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTConsumer.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 23 | #include "clang/AST/Stmt.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/Parse/Parser.h" |
Ted Kremenek | 934a571 | 2011-03-18 03:44:21 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/OwningPtr.h" |
Ted Kremenek | 965fe84 | 2011-03-18 02:06:53 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CrashRecoveryContext.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 27 | #include <cstdio> |
| 28 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Public interface to the file |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 35 | /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 36 | /// the file is parsed. This inserts the parsed decls into the translation unit |
| 37 | /// held by Ctx. |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 38 | /// |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 39 | void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 40 | ASTContext &Ctx, bool PrintStats, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 41 | TranslationUnitKind TUKind, |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 42 | CodeCompleteConsumer *CompletionConsumer, |
| 43 | bool SkipFunctionBodies) { |
Ted Kremenek | 934a571 | 2011-03-18 03:44:21 +0000 | [diff] [blame] | 44 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 45 | OwningPtr<Sema> S(new Sema(PP, Ctx, *Consumer, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 46 | TUKind, |
Ted Kremenek | 934a571 | 2011-03-18 03:44:21 +0000 | [diff] [blame] | 47 | CompletionConsumer)); |
Ted Kremenek | 965fe84 | 2011-03-18 02:06:53 +0000 | [diff] [blame] | 48 | |
| 49 | // Recover resources if we crash before exiting this method. |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 50 | llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get()); |
Ted Kremenek | 965fe84 | 2011-03-18 02:06:53 +0000 | [diff] [blame] | 51 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 52 | ParseAST(*S.get(), PrintStats, SkipFunctionBodies); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 55 | void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) { |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 56 | // Collect global stats on Decls/Stmts (until we have a module streamer). |
| 57 | if (PrintStats) { |
Daniel Dunbar | 02892a6 | 2012-03-05 21:42:49 +0000 | [diff] [blame] | 58 | Decl::EnableStatistics(); |
| 59 | Stmt::EnableStatistics(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 60 | } |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 61 | |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 62 | // Also turn on collection of stats inside of the Sema object. |
| 63 | bool OldCollectStats = PrintStats; |
| 64 | std::swap(OldCollectStats, S.CollectStats); |
| 65 | |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 66 | ASTConsumer *Consumer = &S.getASTConsumer(); |
John McCall | e783d00 | 2010-08-12 21:23:27 +0000 | [diff] [blame] | 67 | |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 68 | OwningPtr<Parser> ParseOP(new Parser(S.getPreprocessor(), S, |
| 69 | SkipFunctionBodies)); |
Ted Kremenek | 614f96a | 2011-03-22 01:15:17 +0000 | [diff] [blame] | 70 | Parser &P = *ParseOP.get(); |
| 71 | |
| 72 | PrettyStackTraceParserEntry CrashInfo(P); |
| 73 | |
| 74 | // Recover resources if we crash before exiting this method. |
| 75 | llvm::CrashRecoveryContextCleanupRegistrar<Parser> |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 76 | CleanupParser(ParseOP.get()); |
Ted Kremenek | 614f96a | 2011-03-22 01:15:17 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 78 | S.getPreprocessor().EnterMainSourceFile(); |
John McCall | d69fd7f | 2010-08-12 21:39:05 +0000 | [diff] [blame] | 79 | P.Initialize(); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 80 | S.Initialize(); |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame^] | 81 | |
| 82 | // C11 6.9p1 says translation units must have at least one top-level |
| 83 | // declaration. C++ doesn't have this restriction. We also don't want to |
| 84 | // complain if we have a precompiled header, although technically if the PCH |
| 85 | // is empty we should still emit the (pedantic) diagnostic. |
| 86 | bool WarnForEmptyTU = !S.getLangOpts().CPlusPlus; |
| 87 | if (ExternalASTSource *External = S.getASTContext().getExternalSource()) { |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 88 | External->StartTranslationUnit(Consumer); |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame^] | 89 | WarnForEmptyTU = false; |
| 90 | } |
| 91 | |
| 92 | // Clang's predefines contain top-level declarations for things like va_list, |
| 93 | // making it hard to tell if the /user's/ translation unit has at least one |
| 94 | // top-level declaration. So we parse cautiously, looking for a declaration |
| 95 | // that doesn't come from our predefines. |
| 96 | // Note that ParseTopLevelDecl returns 'true' at EOF. |
| 97 | SourceManager &SM = S.getSourceManager(); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 98 | Parser::DeclGroupPtrTy ADecl; |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame^] | 99 | while (WarnForEmptyTU && !P.ParseTopLevelDecl(ADecl)) { |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 100 | if (ADecl) { |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame^] | 101 | if (!Consumer->HandleTopLevelDecl(ADecl.get())) |
| 102 | return; |
| 103 | if (DeclGroupRef::iterator FirstDecl = ADecl.get().begin()) { |
| 104 | SourceLocation DeclLoc = (*FirstDecl)->getLocation(); |
| 105 | WarnForEmptyTU = SM.isFromPredefines(DeclLoc); |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame^] | 108 | } |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 109 | |
Jordan Rose | d73ef13 | 2012-06-06 17:25:21 +0000 | [diff] [blame^] | 110 | // If we ended up seeing EOF before any top-level declarations, emit our |
| 111 | // diagnostic. Otherwise, parse the rest of the file normally. |
| 112 | if (WarnForEmptyTU) { |
| 113 | P.Diag(diag::ext_empty_translation_unit); |
| 114 | } else { |
| 115 | while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file. |
| 116 | // If we got a null return and something *was* parsed, ignore it. This |
| 117 | // is due to a top-level semicolon, an action override, or a parse error |
| 118 | // skipping something. |
| 119 | if (ADecl) { |
| 120 | if (!Consumer->HandleTopLevelDecl(ADecl.get())) |
| 121 | return; |
| 122 | } |
| 123 | }; |
| 124 | } |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 125 | |
Daniel Dunbar | ec2a4ed | 2009-12-01 21:57:20 +0000 | [diff] [blame] | 126 | // Process any TopLevelDecls generated by #pragma weak. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 127 | for (SmallVector<Decl*,2>::iterator |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 128 | I = S.WeakTopLevelDecls().begin(), |
| 129 | E = S.WeakTopLevelDecls().end(); I != E; ++I) |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 130 | Consumer->HandleTopLevelDecl(DeclGroupRef(*I)); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 131 | |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 132 | Consumer->HandleTranslationUnit(S.getASTContext()); |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 133 | |
| 134 | std::swap(OldCollectStats, S.CollectStats); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 135 | if (PrintStats) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 136 | llvm::errs() << "\nSTATISTICS:\n"; |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 137 | P.getActions().PrintStats(); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 138 | S.getASTContext().PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 139 | Decl::PrintStats(); |
| 140 | Stmt::PrintStats(); |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 141 | Consumer->PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 142 | } |
| 143 | } |