2008-02-06 08:23:21 +08:00
|
|
|
//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
|
2006-08-17 14:28:25 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-08-17 14:28:25 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-02-06 08:23:21 +08:00
|
|
|
// This file implements the clang::ParseAST method.
|
2006-08-17 14:28:25 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-06 08:23:21 +08:00
|
|
|
#include "clang/Sema/ParseAST.h"
|
2009-04-19 13:30:08 +08:00
|
|
|
#include "Sema.h"
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
llvm-svn: 82166
2009-09-18 05:32:03 +08:00
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
2009-04-20 23:53:59 +08:00
|
|
|
#include "clang/Sema/SemaConsumer.h"
|
Lazy deserialization of the declaration chains associated with
identifiers from a precompiled header.
This patch changes the primary name lookup method for entities within
a precompiled header. Previously, we would load all of the names of
declarations at translation unit scope into a large DenseMap (inside
the TranslationUnitDecl's DeclContext), and then perform a special
"last resort" lookup into this DeclContext when we knew there was a
PCH file (see Sema::LookupName). Now, when we see an identifier named
for the first time, we load all of the declarations with that name
that are visible from the translation unit into the IdentifierInfo's
chain of declarations. Thus, the explicit "look into the translation
unit's DeclContext" code is gone, and Sema effectively uses the same
IdentifierInfo-based name lookup mechanism whether we are using a PCH
file or not.
This approach should help PCH scale with the size of the input program
rather than the size of the PCH file. The "Hello, World!" application
with Carbon.h as a PCH file now loads 20% of the identifiers in the
PCH file rather than 85% of the identifiers.
90% of the 20% of identifiers loaded are actually loaded when we
deserialize the preprocessor state. The next step is to make the
preprocessor load macros lazily, which should drastically reduce the
number of types, declarations, and identifiers loaded for "Hello,
World".
llvm-svn: 69737
2009-04-22 06:25:48 +08:00
|
|
|
#include "clang/Sema/ExternalSemaSource.h"
|
2007-09-16 06:56:56 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-04-14 08:24:19 +08:00
|
|
|
#include "clang/AST/ExternalASTSource.h"
|
2008-08-11 12:54:23 +08:00
|
|
|
#include "clang/AST/Stmt.h"
|
2006-08-17 14:28:25 +08:00
|
|
|
#include "clang/Parse/Parser.h"
|
2009-08-24 21:25:12 +08:00
|
|
|
#include <cstdio>
|
|
|
|
|
2006-08-17 14:28:25 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Public interface to the file
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-09-16 06:56:56 +08:00
|
|
|
/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
|
2009-03-28 12:13:34 +08:00
|
|
|
/// the file is parsed. This inserts the parsed decls into the translation unit
|
|
|
|
/// held by Ctx.
|
2008-10-17 00:54:18 +08:00
|
|
|
///
|
2009-01-28 12:29:29 +08:00
|
|
|
void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
|
2009-04-15 00:27:31 +08:00
|
|
|
ASTContext &Ctx, bool PrintStats,
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
llvm-svn: 82166
2009-09-18 05:32:03 +08:00
|
|
|
bool CompleteTranslationUnit,
|
2009-11-13 16:58:20 +08:00
|
|
|
CodeCompleteConsumer *CompletionConsumer) {
|
2007-09-16 06:56:56 +08:00
|
|
|
// Collect global stats on Decls/Stmts (until we have a module streamer).
|
|
|
|
if (PrintStats) {
|
|
|
|
Decl::CollectingStats(true);
|
|
|
|
Stmt::CollectingStats(true);
|
|
|
|
}
|
2009-01-28 12:29:29 +08:00
|
|
|
|
2009-11-13 16:58:20 +08:00
|
|
|
Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit, CompletionConsumer);
|
2008-05-27 12:23:47 +08:00
|
|
|
Parser P(PP, S);
|
2008-02-06 08:15:02 +08:00
|
|
|
PP.EnterMainSourceFile();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-06 08:15:02 +08:00
|
|
|
// Initialize the parser.
|
|
|
|
P.Initialize();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 12:13:34 +08:00
|
|
|
Consumer->Initialize(Ctx);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-20 23:53:59 +08:00
|
|
|
if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer))
|
|
|
|
SC->InitializeSema(S);
|
|
|
|
|
Lazy deserialization of the declaration chains associated with
identifiers from a precompiled header.
This patch changes the primary name lookup method for entities within
a precompiled header. Previously, we would load all of the names of
declarations at translation unit scope into a large DenseMap (inside
the TranslationUnitDecl's DeclContext), and then perform a special
"last resort" lookup into this DeclContext when we knew there was a
PCH file (see Sema::LookupName). Now, when we see an identifier named
for the first time, we load all of the declarations with that name
that are visible from the translation unit into the IdentifierInfo's
chain of declarations. Thus, the explicit "look into the translation
unit's DeclContext" code is gone, and Sema effectively uses the same
IdentifierInfo-based name lookup mechanism whether we are using a PCH
file or not.
This approach should help PCH scale with the size of the input program
rather than the size of the PCH file. The "Hello, World!" application
with Carbon.h as a PCH file now loads 20% of the identifiers in the
PCH file rather than 85% of the identifiers.
90% of the 20% of identifiers loaded are actually loaded when we
deserialize the preprocessor state. The next step is to make the
preprocessor load macros lazily, which should drastically reduce the
number of types, declarations, and identifiers loaded for "Hello,
World".
llvm-svn: 69737
2009-04-22 06:25:48 +08:00
|
|
|
if (ExternalASTSource *External = Ctx.getExternalSource()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ExternalSemaSource *ExternalSema =
|
Lazy deserialization of the declaration chains associated with
identifiers from a precompiled header.
This patch changes the primary name lookup method for entities within
a precompiled header. Previously, we would load all of the names of
declarations at translation unit scope into a large DenseMap (inside
the TranslationUnitDecl's DeclContext), and then perform a special
"last resort" lookup into this DeclContext when we knew there was a
PCH file (see Sema::LookupName). Now, when we see an identifier named
for the first time, we load all of the declarations with that name
that are visible from the translation unit into the IdentifierInfo's
chain of declarations. Thus, the explicit "look into the translation
unit's DeclContext" code is gone, and Sema effectively uses the same
IdentifierInfo-based name lookup mechanism whether we are using a PCH
file or not.
This approach should help PCH scale with the size of the input program
rather than the size of the PCH file. The "Hello, World!" application
with Carbon.h as a PCH file now loads 20% of the identifiers in the
PCH file rather than 85% of the identifiers.
90% of the 20% of identifiers loaded are actually loaded when we
deserialize the preprocessor state. The next step is to make the
preprocessor load macros lazily, which should drastically reduce the
number of types, declarations, and identifiers loaded for "Hello,
World".
llvm-svn: 69737
2009-04-22 06:25:48 +08:00
|
|
|
dyn_cast<ExternalSemaSource>(External))
|
|
|
|
ExternalSema->InitializeSema(S);
|
|
|
|
|
|
|
|
External->StartTranslationUnit(Consumer);
|
|
|
|
}
|
2009-04-14 08:24:19 +08:00
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
Parser::DeclGroupPtrTy ADecl;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-06 08:15:02 +08:00
|
|
|
while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
|
|
|
|
// If we got a null return and something *was* parsed, ignore it. This
|
|
|
|
// is due to a top-level semicolon, an action override, or a parse error
|
|
|
|
// skipping something.
|
2009-03-30 00:50:03 +08:00
|
|
|
if (ADecl)
|
|
|
|
Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
|
2008-02-06 08:15:02 +08:00
|
|
|
};
|
2009-11-17 02:57:01 +08:00
|
|
|
// Check for any pending objective-c implementation decl.
|
2009-11-18 01:15:16 +08:00
|
|
|
while ((ADecl = P.RetrievePendingObjCImpDecl()))
|
2009-11-17 02:57:01 +08:00
|
|
|
Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-02 05:57:20 +08:00
|
|
|
// Process any TopLevelDecls generated by #pragma weak.
|
2009-07-31 10:52:19 +08:00
|
|
|
for (llvm::SmallVector<Decl*,2>::iterator
|
|
|
|
I = S.WeakTopLevelDecls().begin(),
|
|
|
|
E = S.WeakTopLevelDecls().end(); I != E; ++I)
|
|
|
|
Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
|
|
|
|
|
2009-03-28 12:13:34 +08:00
|
|
|
Consumer->HandleTranslationUnit(Ctx);
|
2007-10-27 03:46:17 +08:00
|
|
|
|
2009-12-02 05:57:20 +08:00
|
|
|
if (ExternalSemaSource *ESS =
|
|
|
|
dyn_cast_or_null<ExternalSemaSource>(Ctx.getExternalSource()))
|
|
|
|
ESS->ForgetSema();
|
|
|
|
|
|
|
|
if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer))
|
|
|
|
SC->ForgetSema();
|
|
|
|
|
2007-09-16 06:56:56 +08:00
|
|
|
if (PrintStats) {
|
|
|
|
fprintf(stderr, "\nSTATISTICS:\n");
|
2008-02-06 08:15:02 +08:00
|
|
|
P.getActions().PrintStats();
|
2009-03-28 12:13:34 +08:00
|
|
|
Ctx.PrintStats();
|
2007-09-16 06:56:56 +08:00
|
|
|
Decl::PrintStats();
|
|
|
|
Stmt::PrintStats();
|
2007-11-03 14:24:16 +08:00
|
|
|
Consumer->PrintStats();
|
2007-09-16 06:56:56 +08:00
|
|
|
}
|
|
|
|
}
|