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