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" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Sema.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaConsumer.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ExternalSemaSource.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExternalASTSource.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 22 | #include "clang/AST/Stmt.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "clang/Parse/Parser.h" |
Ted Kremenek | 934a571 | 2011-03-18 03:44:21 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/OwningPtr.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> |
| 27 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Public interface to the file |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 34 | /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 35 | /// the file is parsed. This inserts the parsed decls into the translation unit |
| 36 | /// held by Ctx. |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 37 | /// |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 38 | void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 39 | ASTContext &Ctx, bool PrintStats, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 40 | TranslationUnitKind TUKind, |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 41 | CodeCompleteConsumer *CompletionConsumer) { |
Ted Kremenek | 934a571 | 2011-03-18 03:44:21 +0000 | [diff] [blame] | 42 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 43 | OwningPtr<Sema> S(new Sema(PP, Ctx, *Consumer, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 44 | TUKind, |
Ted Kremenek | 934a571 | 2011-03-18 03:44:21 +0000 | [diff] [blame] | 45 | CompletionConsumer)); |
Ted Kremenek | 965fe84 | 2011-03-18 02:06:53 +0000 | [diff] [blame] | 46 | |
| 47 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 614f96a | 2011-03-22 01:15:17 +0000 | [diff] [blame] | 48 | llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleaupSema(S.get()); |
Ted Kremenek | 965fe84 | 2011-03-18 02:06:53 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | 934a571 | 2011-03-18 03:44:21 +0000 | [diff] [blame] | 50 | ParseAST(*S.get(), PrintStats); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void clang::ParseAST(Sema &S, bool PrintStats) { |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 54 | // Collect global stats on Decls/Stmts (until we have a module streamer). |
| 55 | if (PrintStats) { |
| 56 | Decl::CollectingStats(true); |
| 57 | Stmt::CollectingStats(true); |
| 58 | } |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 59 | |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 60 | // Also turn on collection of stats inside of the Sema object. |
| 61 | bool OldCollectStats = PrintStats; |
| 62 | std::swap(OldCollectStats, S.CollectStats); |
| 63 | |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 64 | ASTConsumer *Consumer = &S.getASTConsumer(); |
John McCall | e783d00 | 2010-08-12 21:23:27 +0000 | [diff] [blame] | 65 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 66 | OwningPtr<Parser> ParseOP(new Parser(S.getPreprocessor(), S)); |
Ted Kremenek | 614f96a | 2011-03-22 01:15:17 +0000 | [diff] [blame] | 67 | Parser &P = *ParseOP.get(); |
| 68 | |
| 69 | PrettyStackTraceParserEntry CrashInfo(P); |
| 70 | |
| 71 | // Recover resources if we crash before exiting this method. |
| 72 | llvm::CrashRecoveryContextCleanupRegistrar<Parser> |
| 73 | CleaupParser(ParseOP.get()); |
| 74 | |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 75 | S.getPreprocessor().EnterMainSourceFile(); |
John McCall | d69fd7f | 2010-08-12 21:39:05 +0000 | [diff] [blame] | 76 | P.Initialize(); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 77 | S.Initialize(); |
| 78 | |
| 79 | if (ExternalASTSource *External = S.getASTContext().getExternalSource()) |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 80 | External->StartTranslationUnit(Consumer); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 81 | |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 82 | bool Abort = false; |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 83 | Parser::DeclGroupPtrTy ADecl; |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 84 | |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 85 | while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file. |
John McCall | d69fd7f | 2010-08-12 21:39:05 +0000 | [diff] [blame] | 86 | // If we got a null return and something *was* parsed, ignore it. This |
| 87 | // is due to a top-level semicolon, an action override, or a parse error |
| 88 | // skipping something. |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 89 | if (ADecl) { |
| 90 | if (!Consumer->HandleTopLevelDecl(ADecl.get())) { |
| 91 | Abort = true; |
| 92 | break; |
| 93 | } |
| 94 | } |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 95 | }; |
Argyrios Kyrtzidis | 88c2596 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 96 | |
| 97 | if (Abort) |
| 98 | return; |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 99 | |
Daniel Dunbar | ec2a4ed | 2009-12-01 21:57:20 +0000 | [diff] [blame] | 100 | // Process any TopLevelDecls generated by #pragma weak. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 101 | for (SmallVector<Decl*,2>::iterator |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 102 | I = S.WeakTopLevelDecls().begin(), |
| 103 | E = S.WeakTopLevelDecls().end(); I != E; ++I) |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 104 | Consumer->HandleTopLevelDecl(DeclGroupRef(*I)); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 105 | |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 106 | Consumer->HandleTranslationUnit(S.getASTContext()); |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 107 | |
| 108 | std::swap(OldCollectStats, S.CollectStats); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 109 | if (PrintStats) { |
Chandler Carruth | cd92a65 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 110 | llvm::errs() << "\nSTATISTICS:\n"; |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 111 | P.getActions().PrintStats(); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 112 | S.getASTContext().PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 113 | Decl::PrintStats(); |
| 114 | Stmt::PrintStats(); |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 115 | Consumer->PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 116 | } |
| 117 | } |