clang API Documentation
00001 //===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the clang::ParseAST method. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/Parse/ParseAST.h" 00015 #include "clang/Sema/Sema.h" 00016 #include "clang/Sema/CodeCompleteConsumer.h" 00017 #include "clang/Sema/SemaConsumer.h" 00018 #include "clang/Sema/ExternalSemaSource.h" 00019 #include "clang/AST/ASTConsumer.h" 00020 #include "clang/AST/DeclCXX.h" 00021 #include "clang/AST/ExternalASTSource.h" 00022 #include "clang/AST/Stmt.h" 00023 #include "clang/Parse/Parser.h" 00024 #include "llvm/ADT/OwningPtr.h" 00025 #include "llvm/Support/CrashRecoveryContext.h" 00026 #include <cstdio> 00027 00028 using namespace clang; 00029 00030 //===----------------------------------------------------------------------===// 00031 // Public interface to the file 00032 //===----------------------------------------------------------------------===// 00033 00034 /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as 00035 /// the file is parsed. This inserts the parsed decls into the translation unit 00036 /// held by Ctx. 00037 /// 00038 void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, 00039 ASTContext &Ctx, bool PrintStats, 00040 TranslationUnitKind TUKind, 00041 CodeCompleteConsumer *CompletionConsumer, 00042 bool SkipFunctionBodies) { 00043 00044 OwningPtr<Sema> S(new Sema(PP, Ctx, *Consumer, 00045 TUKind, 00046 CompletionConsumer)); 00047 00048 // Recover resources if we crash before exiting this method. 00049 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(S.get()); 00050 00051 ParseAST(*S.get(), PrintStats, SkipFunctionBodies); 00052 } 00053 00054 void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) { 00055 // Collect global stats on Decls/Stmts (until we have a module streamer). 00056 if (PrintStats) { 00057 Decl::EnableStatistics(); 00058 Stmt::EnableStatistics(); 00059 } 00060 00061 // Also turn on collection of stats inside of the Sema object. 00062 bool OldCollectStats = PrintStats; 00063 std::swap(OldCollectStats, S.CollectStats); 00064 00065 ASTConsumer *Consumer = &S.getASTConsumer(); 00066 00067 OwningPtr<Parser> ParseOP(new Parser(S.getPreprocessor(), S, 00068 SkipFunctionBodies)); 00069 Parser &P = *ParseOP.get(); 00070 00071 PrettyStackTraceParserEntry CrashInfo(P); 00072 00073 // Recover resources if we crash before exiting this method. 00074 llvm::CrashRecoveryContextCleanupRegistrar<Parser> 00075 CleanupParser(ParseOP.get()); 00076 00077 S.getPreprocessor().EnterMainSourceFile(); 00078 P.Initialize(); 00079 S.Initialize(); 00080 00081 if (ExternalASTSource *External = S.getASTContext().getExternalSource()) 00082 External->StartTranslationUnit(Consumer); 00083 00084 bool Abort = false; 00085 Parser::DeclGroupPtrTy ADecl; 00086 00087 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file. 00088 // If we got a null return and something *was* parsed, ignore it. This 00089 // is due to a top-level semicolon, an action override, or a parse error 00090 // skipping something. 00091 if (ADecl) { 00092 if (!Consumer->HandleTopLevelDecl(ADecl.get())) { 00093 Abort = true; 00094 break; 00095 } 00096 } 00097 }; 00098 00099 if (Abort) 00100 return; 00101 00102 // Process any TopLevelDecls generated by #pragma weak. 00103 for (SmallVector<Decl*,2>::iterator 00104 I = S.WeakTopLevelDecls().begin(), 00105 E = S.WeakTopLevelDecls().end(); I != E; ++I) 00106 Consumer->HandleTopLevelDecl(DeclGroupRef(*I)); 00107 00108 Consumer->HandleTranslationUnit(S.getASTContext()); 00109 00110 std::swap(OldCollectStats, S.CollectStats); 00111 if (PrintStats) { 00112 llvm::errs() << "\nSTATISTICS:\n"; 00113 P.getActions().PrintStats(); 00114 S.getASTContext().PrintStats(); 00115 Decl::PrintStats(); 00116 Stmt::PrintStats(); 00117 Consumer->PrintStats(); 00118 } 00119 }