Introduce a -cc1 option "-emit-module", that creates a binary module

from the given source. -emit-module behaves similarly to -emit-pch,
except that Sema is somewhat more strict about the contents of
-emit-module. In the future, there are likely to be more interesting
differences.

llvm-svn: 138595
This commit is contained in:
Douglas Gregor 2011-08-25 22:30:56 +00:00
parent 371376010f
commit 69f74f80db
25 changed files with 99 additions and 67 deletions

View File

@ -296,6 +296,18 @@ public:
}
};
/// \brief Describes the kind of translation unit being processed.
enum TranslationUnitKind {
/// \brief The translation unit is a complete translation unit.
TU_Complete,
/// \brief The translation unit is a prefix to a translation unit, and is
/// not complete.
TU_Prefix,
/// \brief The translation unit is a module.
TU_Module
};
/// \brief
} // end namespace clang
#endif

View File

@ -360,6 +360,8 @@ def ast_view : Flag<"-ast-view">,
HelpText<"Build ASTs and view them with GraphViz">;
def print_decl_contexts : Flag<"-print-decl-contexts">,
HelpText<"Print DeclContexts and their Decls">;
def emit_module : Flag<"-emit-module">,
HelpText<"Generate pre-compiled module file">;
def emit_pth : Flag<"-emit-pth">,
HelpText<"Generate pre-tokenized header file">;
def emit_pch : Flag<"-emit-pch">,

View File

@ -112,8 +112,8 @@ private:
/// \brief Track whether the main file was loaded from an AST or not.
bool MainFileIsAST;
/// \brief Whether this AST represents a complete translation unit.
bool CompleteTranslationUnit;
/// \brief What kind of translation unit this AST represents.
TranslationUnitKind TUKind;
/// \brief Whether we should time each operation.
bool WantTiming;
@ -548,11 +548,8 @@ public:
llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
std::string *ErrorStr = 0);
/// \brief Whether this AST represents a complete translation unit.
///
/// If false, this AST is only a partial translation unit, e.g., one
/// that might still be used as a precompiled header or preamble.
bool isCompleteTranslationUnit() const { return CompleteTranslationUnit; }
/// \brief Determine what kind of translation unit this AST represents.
TranslationUnitKind getTranslationUnitKind() const { return TUKind; }
typedef llvm::PointerUnion<const char *, const llvm::MemoryBuffer *>
FilenameOrMemBuf;
@ -624,7 +621,7 @@ public:
bool OnlyLocalDecls = false,
bool CaptureDiagnostics = false,
bool PrecompilePreamble = false,
bool CompleteTranslationUnit = true,
TranslationUnitKind TUKind = TU_Complete,
bool CacheCodeCompletionResults = false,
bool NestedMacroExpansions = true);
@ -652,7 +649,7 @@ public:
unsigned NumRemappedFiles = 0,
bool RemappedFilesKeepOriginalName = true,
bool PrecompilePreamble = false,
bool CompleteTranslationUnit = true,
TranslationUnitKind TUKind = TU_Complete,
bool CacheCodeCompletionResults = false,
bool CXXPrecompilePreamble = false,
bool CXXChainedPCH = false,

View File

@ -548,7 +548,7 @@ public:
raw_ostream &OS);
/// \brief Create the Sema object to be used for parsing.
void createSema(bool CompleteTranslationUnit,
void createSema(TranslationUnitKind TUKind,
CodeCompleteConsumer *CompletionConsumer);
/// Create the frontend timer and replace any existing one with it.

View File

@ -11,6 +11,7 @@
#define LLVM_CLANG_FRONTEND_FRONTENDACTION_H
#include "clang/Basic/LLVM.h"
#include "clang/Basic/LangOptions.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/OwningPtr.h"
#include <string>
@ -160,9 +161,8 @@ public:
/// file inputs.
virtual bool usesPreprocessorOnly() const = 0;
/// usesCompleteTranslationUnit - For AST based actions, should the
/// translation unit be completed?
virtual bool usesCompleteTranslationUnit() { return true; }
/// \brief For AST-based actions, the kind of translation unit we're handling.
virtual TranslationUnitKind getTranslationUnitKind() { return TU_Complete; }
/// hasPCHSupport - Does this action support use with PCH?
virtual bool hasPCHSupport() const { return !usesPreprocessorOnly(); }
@ -282,7 +282,7 @@ public:
WrapperFrontendAction(FrontendAction *WrappedAction);
virtual bool usesPreprocessorOnly() const;
virtual bool usesCompleteTranslationUnit();
virtual TranslationUnitKind getTranslationUnitKind();
virtual bool hasPCHSupport() const;
virtual bool hasASTFileSupport() const;
virtual bool hasIRSupport() const;

View File

@ -67,15 +67,22 @@ protected:
};
class GeneratePCHAction : public ASTFrontendAction {
bool MakeModule;
protected:
virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
StringRef InFile);
virtual bool usesCompleteTranslationUnit() { return false; }
virtual TranslationUnitKind getTranslationUnitKind() {
return MakeModule? TU_Module : TU_Prefix;
}
virtual bool hasASTFileSupport() const { return false; }
public:
/// \brief Create a new action
explicit GeneratePCHAction(bool MakeModule) : MakeModule(MakeModule) { }
/// \brief Compute the AST consumer arguments that will be used to
/// create the PCHGenerator instance returned by CreateASTConsumer.
///
@ -128,7 +135,7 @@ public:
virtual ~ASTMergeAction();
virtual bool usesPreprocessorOnly() const;
virtual bool usesCompleteTranslationUnit();
virtual TranslationUnitKind getTranslationUnitKind();
virtual bool hasPCHSupport() const;
virtual bool hasASTFileSupport() const;
virtual bool hasCodeCompletionSupport() const;

View File

@ -35,6 +35,7 @@ namespace frontend {
EmitCodeGenOnly, ///< Generate machine code, but don't emit anything.
EmitObj, ///< Emit a .o file.
FixIt, ///< Parse and apply any fixits to the source.
GenerateModule, ///< Generate pre-compiled module.
GeneratePCH, ///< Generate pre-compiled header.
GeneratePTH, ///< Generate pre-tokenized header.
InitOnly, ///< Only execute frontend initialization.

View File

@ -14,6 +14,8 @@
#ifndef LLVM_CLANG_PARSE_PARSEAST_H
#define LLVM_CLANG_PARSE_PARSEAST_H
#include "clang/Basic/LangOptions.h"
namespace clang {
class Preprocessor;
class ASTConsumer;
@ -27,15 +29,13 @@ namespace clang {
/// This operation inserts the parsed decls into the translation
/// unit held by Ctx.
///
/// \param CompleteTranslationUnit When true, the parsed file is
/// considered to be a complete translation unit, and any
/// end-of-translation-unit wrapup will be performed.
/// \param TUKind The kind of translation unit being parsed.
///
/// \param CompletionConsumer If given, an object to consume code completion
/// results.
void ParseAST(Preprocessor &pp, ASTConsumer *C,
ASTContext &Ctx, bool PrintStats = false,
bool CompleteTranslationUnit = true,
TranslationUnitKind TUKind = TU_Complete,
CodeCompleteConsumer *CompletionConsumer = 0);
/// \brief Parse the main file known to the preprocessor, producing an

View File

@ -630,16 +630,14 @@ public:
/// for C++ records.
llvm::FoldingSet<SpecialMemberOverloadResult> SpecialMemberCache;
/// \brief Whether the code handled by Sema should be considered a
/// complete translation unit or not.
/// \brief The kind of translation unit we are processing.
///
/// When true (which is generally the case), Sema will perform
/// When we're processing a complete translation unit, Sema will perform
/// end-of-translation-unit semantic tasks (such as creating
/// initializers for tentative definitions in C) once parsing has
/// completed. This flag will be false when building PCH files,
/// since a PCH file is by definition not a complete translation
/// unit.
bool CompleteTranslationUnit;
/// completed. Modules and precompiled headers perform different kinds of
/// checks.
TranslationUnitKind TUKind;
llvm::BumpPtrAllocator BumpAlloc;
@ -685,7 +683,7 @@ public:
bool isSelfExpr(Expr *RExpr);
public:
Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
bool CompleteTranslationUnit = true,
TranslationUnitKind TUKind = TU_Complete,
CodeCompleteConsumer *CompletionConsumer = 0);
~Sema();

View File

@ -641,7 +641,7 @@ protected:
const ASTWriter &getWriter() const { return Writer; }
public:
PCHGenerator(const Preprocessor &PP, const std::string &OutputFile,
PCHGenerator(const Preprocessor &PP, StringRef OutputFile,
bool Chaining, StringRef isysroot, raw_ostream *Out);
~PCHGenerator();
virtual void InitializeSema(Sema &S) { SemaPtr = &S; }

View File

@ -93,8 +93,8 @@ bool ASTMergeAction::usesPreprocessorOnly() const {
return AdaptedAction->usesPreprocessorOnly();
}
bool ASTMergeAction::usesCompleteTranslationUnit() {
return AdaptedAction->usesCompleteTranslationUnit();
TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
return AdaptedAction->getTranslationUnitKind();
}
bool ASTMergeAction::hasPCHSupport() const {

View File

@ -97,7 +97,7 @@ static llvm::sys::cas_flag ActiveASTUnitObjects;
ASTUnit::ASTUnit(bool _MainFileIsAST)
: OnlyLocalDecls(false), CaptureDiagnostics(false),
MainFileIsAST(_MainFileIsAST),
CompleteTranslationUnit(true), WantTiming(getenv("LIBCLANG_TIMING")),
TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")),
OwnsRemappedFileBuffers(true),
NumStoredDiagnosticsFromDriver(0),
ConcurrencyCheckValue(CheckUnlocked),
@ -759,8 +759,8 @@ public:
TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
virtual bool hasCodeCompletionSupport() const { return false; }
virtual bool usesCompleteTranslationUnit() {
return Unit.isCompleteTranslationUnit();
virtual TranslationUnitKind getTranslationUnitKind() {
return Unit.getTranslationUnitKind();
}
};
@ -844,7 +844,7 @@ public:
virtual bool hasCodeCompletionSupport() const { return false; }
virtual bool hasASTFileSupport() const { return false; }
virtual bool usesCompleteTranslationUnit() { return false; }
virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; }
};
}
@ -1592,8 +1592,7 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI,
AST->Diagnostics = Diags;
AST->OnlyLocalDecls = false;
AST->CaptureDiagnostics = false;
AST->CompleteTranslationUnit = Action ? Action->usesCompleteTranslationUnit()
: true;
AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete;
AST->ShouldCacheCodeCompletionResults = false;
AST->Invocation = CI;
@ -1727,7 +1726,7 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
bool OnlyLocalDecls,
bool CaptureDiagnostics,
bool PrecompilePreamble,
bool CompleteTranslationUnit,
TranslationUnitKind TUKind,
bool CacheCodeCompletionResults,
bool NestedMacroExpansions) {
// Create the AST unit.
@ -1737,7 +1736,7 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
AST->Diagnostics = Diags;
AST->OnlyLocalDecls = OnlyLocalDecls;
AST->CaptureDiagnostics = CaptureDiagnostics;
AST->CompleteTranslationUnit = CompleteTranslationUnit;
AST->TUKind = TUKind;
AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
AST->Invocation = CI;
AST->NestedMacroExpansions = NestedMacroExpansions;
@ -1762,7 +1761,7 @@ ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
unsigned NumRemappedFiles,
bool RemappedFilesKeepOriginalName,
bool PrecompilePreamble,
bool CompleteTranslationUnit,
TranslationUnitKind TUKind,
bool CacheCodeCompletionResults,
bool CXXPrecompilePreamble,
bool CXXChainedPCH,
@ -1828,7 +1827,7 @@ ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
AST->FileMgr = new FileManager(AST->FileSystemOpts);
AST->OnlyLocalDecls = OnlyLocalDecls;
AST->CaptureDiagnostics = CaptureDiagnostics;
AST->CompleteTranslationUnit = CompleteTranslationUnit;
AST->TUKind = TUKind;
AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults;
AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size();
AST->StoredDiagnostics.swap(StoredDiagnostics);

View File

@ -382,10 +382,10 @@ CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
ShowGlobals, OS);
}
void CompilerInstance::createSema(bool CompleteTranslationUnit,
void CompilerInstance::createSema(TranslationUnitKind TUKind,
CodeCompleteConsumer *CompletionConsumer) {
TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
CompleteTranslationUnit, CompletionConsumer));
TUKind, CompletionConsumer));
}
// Output Files

View File

@ -372,6 +372,7 @@ static const char *getActionName(frontend::ActionKind Kind) {
case frontend::EmitCodeGenOnly: return "-emit-codegen-only";
case frontend::EmitObj: return "-emit-obj";
case frontend::FixIt: return "-fixit";
case frontend::GenerateModule: return "-emit-module";
case frontend::GeneratePCH: return "-emit-pch";
case frontend::GeneratePTH: return "-emit-pth";
case frontend::InitOnly: return "-init-only";
@ -1205,6 +1206,8 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
// fall-through!
case OPT_fixit:
Opts.ProgramAction = frontend::FixIt; break;
case OPT_emit_module:
Opts.ProgramAction = frontend::GenerateModule; break;
case OPT_emit_pch:
Opts.ProgramAction = frontend::GeneratePCH; break;
case OPT_emit_pth:

View File

@ -398,7 +398,7 @@ void ASTFrontendAction::ExecuteAction() {
CompletionConsumer = &CI.getCodeCompletionConsumer();
if (!CI.hasSema())
CI.createSema(usesCompleteTranslationUnit(), CompletionConsumer);
CI.createSema(getTranslationUnitKind(), CompletionConsumer);
ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats);
}
@ -432,8 +432,8 @@ void WrapperFrontendAction::EndSourceFileAction() {
bool WrapperFrontendAction::usesPreprocessorOnly() const {
return WrappedAction->usesPreprocessorOnly();
}
bool WrapperFrontendAction::usesCompleteTranslationUnit() {
return WrappedAction->usesCompleteTranslationUnit();
TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
return WrappedAction->getTranslationUnitKind();
}
bool WrapperFrontendAction::hasPCHSupport() const {
return WrappedAction->hasPCHSupport();

View File

@ -79,12 +79,13 @@ ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
std::string OutputFile;
raw_ostream *OS = 0;
bool Chaining;
if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS, Chaining))
if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS,
Chaining))
return 0;
if (!CI.getFrontendOpts().RelocatablePCH)
Sysroot.clear();
return new PCHGenerator(CI.getPreprocessor(), OutputFile, Chaining, Sysroot,
return new PCHGenerator(CI.getPreprocessor(), OutputFile, Chaining, Sysroot,
OS);
}

View File

@ -50,7 +50,8 @@ static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
case EmitObj: return new EmitObjAction();
case FixIt: return new FixItAction();
case GeneratePCH: return new GeneratePCHAction();
case GenerateModule: return new GeneratePCHAction(true);
case GeneratePCH: return new GeneratePCHAction(false);
case GeneratePTH: return new GeneratePTHAction();
case InitOnly: return new InitOnlyAction();
case ParseSyntaxOnly: return new SyntaxOnlyAction();

View File

@ -37,11 +37,11 @@ using namespace clang;
///
void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
ASTContext &Ctx, bool PrintStats,
bool CompleteTranslationUnit,
TranslationUnitKind TUKind,
CodeCompleteConsumer *CompletionConsumer) {
llvm::OwningPtr<Sema> S(new Sema(PP, Ctx, *Consumer,
CompleteTranslationUnit,
TUKind,
CompletionConsumer));
// Recover resources if we crash before exiting this method.

View File

@ -74,7 +74,7 @@ void Sema::ActOnTranslationUnitScope(Scope *S) {
}
Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
bool CompleteTranslationUnit,
TranslationUnitKind TUKind,
CodeCompleteConsumer *CodeCompleter)
: TheTargetAttributesSema(0), FPFeatures(pp.getLangOptions()),
LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
@ -85,7 +85,7 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
IdResolver(pp.getLangOptions()), CXXTypeInfoDecl(0), MSVCGuidDecl(0),
GlobalNewDeleteDeclared(false),
ObjCShouldCallSuperDealloc(false),
CompleteTranslationUnit(CompleteTranslationUnit),
TUKind(TUKind),
NumSFINAEErrors(0), SuppressAccessChecking(false),
AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false),
NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1),
@ -391,9 +391,9 @@ void Sema::LoadExternalWeakUndeclaredIdentifiers() {
/// translation unit when EOF is reached and all but the top-level scope is
/// popped.
void Sema::ActOnEndOfTranslationUnit() {
// At PCH writing, implicit instantiations and VTable handling info are
// stored and performed when the PCH is included.
if (CompleteTranslationUnit) {
// Only complete translation units define vtables and perform implicit
// instantiations.
if (TUKind == TU_Complete) {
// If any dynamic classes have their key function defined within
// this translation unit, then those vtables are considered "used" and must
// be emitted.
@ -435,7 +435,8 @@ void Sema::ActOnEndOfTranslationUnit() {
this)),
UnusedFileScopedDecls.end());
if (!CompleteTranslationUnit) {
if (TUKind == TU_Prefix) {
// Translation unit prefixes don't need any of the checking below.
TUScope = 0;
return;
}
@ -453,6 +454,12 @@ void Sema::ActOnEndOfTranslationUnit() {
<< I->first;
}
if (TUKind == TU_Module) {
// Modules don't need any of the checking below.
TUScope = 0;
return;
}
// C99 6.9.2p2:
// A declaration of an identifier for an object that has file
// scope without an initializer, and without a storage-class

View File

@ -111,7 +111,7 @@ ChainedIncludesSource *ChainedIncludesSource::create(CompilerInstance &CI) {
Clang->getASTContext().setASTMutationListener(
consumer->GetASTMutationListener());
Clang->setASTConsumer(consumer.take());
Clang->createSema(/*CompleteTranslationUnit=*/false, 0);
Clang->createSema(TU_Prefix, 0);
if (firstInclude) {
Preprocessor &PP = Clang->getPreprocessor();

View File

@ -27,7 +27,7 @@
using namespace clang;
PCHGenerator::PCHGenerator(const Preprocessor &PP,
const std::string &OutputFile,
StringRef OutputFile,
bool Chaining,
StringRef isysroot,
raw_ostream *OS)

View File

@ -1 +1,5 @@
int *f0(int*);
#pragma weak weak_identifier // expected-warning{{weak identifier 'weak_identifier' never declared}}

View File

@ -1,4 +1,3 @@
void test(int i, float f) {
// unqualified lookup
f0(&i);
@ -9,8 +8,8 @@ void test(int i, float f) {
::f0(&f);
}
// RUN: %clang_cc1 -emit-pch -x c++ -o %t_lookup_left.h.pch %S/Inputs/lookup_left.hpp
// RUN: %clang_cc1 -emit-pch -x c++ -o %t_lookup_right.h.pch %S/Inputs/lookup_right.hpp
// RUN: %clang_cc1 -emit-module -x c++ -verify -o %t_lookup_left.h.pch %S/Inputs/lookup_left.hpp
// RUN: %clang_cc1 -emit-module -x c++ -o %t_lookup_right.h.pch %S/Inputs/lookup_right.hpp
// RUN: %clang_cc1 -x c++ -import-module %t_lookup_left.h.pch -import-module %t_lookup_right.h.pch -verify %s
// RUN: %clang_cc1 -ast-print -x c++ -import-module %t_lookup_left.h.pch -import-module %t_lookup_right.h.pch %s | FileCheck -check-prefix=CHECK-PRINT %s

View File

@ -6,8 +6,8 @@ void test(id x) {
[x method]; // expected-warning{{multiple methods named 'method' found}}
}
// RUN: %clang_cc1 -emit-pch -x objective-c -o %t_lookup_left.h.pch %S/Inputs/lookup_left.h
// RUN: %clang_cc1 -emit-pch -x objective-c -o %t_lookup_right.h.pch %S/Inputs/lookup_right.h
// RUN: %clang_cc1 -emit-module -x objective-c -o %t_lookup_left.h.pch %S/Inputs/lookup_left.h
// RUN: %clang_cc1 -emit-module -x objective-c -o %t_lookup_right.h.pch %S/Inputs/lookup_right.h
// RUN: %clang_cc1 -x objective-c -import-module %t_lookup_left.h.pch -import-module %t_lookup_right.h.pch -verify %s
// RUN: %clang_cc1 -ast-print -x objective-c -import-module %t_lookup_left.h.pch -import-module %t_lookup_right.h.pch %s | FileCheck -check-prefix=CHECK-PRINT %s

View File

@ -2462,8 +2462,9 @@ static void clang_parseTranslationUnit_Impl(void *UserData) {
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
bool CompleteTranslationUnit
= ((options & CXTranslationUnit_Incomplete) == 0);
// FIXME: Add a flag for modules.
TranslationUnitKind TUKind
= (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete;
bool CacheCodeCompetionResults
= options & CXTranslationUnit_CacheCompletionResults;
bool CXXPrecompilePreamble
@ -2553,7 +2554,7 @@ static void clang_parseTranslationUnit_Impl(void *UserData) {
RemappedFiles->size(),
/*RemappedFilesKeepOriginalName=*/true,
PrecompilePreamble,
CompleteTranslationUnit,
TUKind,
CacheCodeCompetionResults,
CXXPrecompilePreamble,
CXXChainedPCH,