2009-11-14 18:42:35 +08:00
|
|
|
//===--- FrontendAction.cpp -----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
2010-07-30 08:29:29 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-11-14 18:42:35 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2011-01-26 04:34:14 +08:00
|
|
|
#include "clang/AST/DeclGroup.h"
|
2009-11-14 18:42:35 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2011-12-09 09:55:54 +08:00
|
|
|
#include "clang/Frontend/ChainedIncludesSource.h"
|
2009-11-14 18:42:35 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2011-01-26 04:34:14 +08:00
|
|
|
#include "clang/Frontend/FrontendPluginRegistry.h"
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
#include "clang/Frontend/LayoutOverrideSource.h"
|
2011-01-26 04:34:14 +08:00
|
|
|
#include "clang/Frontend/MultiplexConsumer.h"
|
2010-08-21 02:27:03 +08:00
|
|
|
#include "clang/Parse/ParseAST.h"
|
2010-10-15 04:14:18 +08:00
|
|
|
#include "clang/Serialization/ASTDeserializationListener.h"
|
2011-08-06 06:17:03 +08:00
|
|
|
#include "clang/Serialization/ASTReader.h"
|
2009-11-14 18:42:35 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
2010-10-15 04:14:18 +08:00
|
|
|
namespace {
|
|
|
|
|
2011-10-29 06:54:31 +08:00
|
|
|
class DelegatingDeserializationListener : public ASTDeserializationListener {
|
2010-10-15 04:14:18 +08:00
|
|
|
ASTDeserializationListener *Previous;
|
|
|
|
|
|
|
|
public:
|
2011-10-29 06:54:31 +08:00
|
|
|
explicit DelegatingDeserializationListener(
|
|
|
|
ASTDeserializationListener *Previous)
|
2010-10-15 04:14:18 +08:00
|
|
|
: Previous(Previous) { }
|
|
|
|
|
2011-10-29 06:54:31 +08:00
|
|
|
virtual void ReaderInitialized(ASTReader *Reader) {
|
|
|
|
if (Previous)
|
|
|
|
Previous->ReaderInitialized(Reader);
|
|
|
|
}
|
|
|
|
virtual void IdentifierRead(serialization::IdentID ID,
|
|
|
|
IdentifierInfo *II) {
|
|
|
|
if (Previous)
|
|
|
|
Previous->IdentifierRead(ID, II);
|
|
|
|
}
|
|
|
|
virtual void TypeRead(serialization::TypeIdx Idx, QualType T) {
|
|
|
|
if (Previous)
|
|
|
|
Previous->TypeRead(Idx, T);
|
|
|
|
}
|
|
|
|
virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
|
|
|
|
if (Previous)
|
|
|
|
Previous->DeclRead(ID, D);
|
|
|
|
}
|
|
|
|
virtual void SelectorRead(serialization::SelectorID ID, Selector Sel) {
|
|
|
|
if (Previous)
|
|
|
|
Previous->SelectorRead(ID, Sel);
|
|
|
|
}
|
|
|
|
virtual void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
|
|
|
|
MacroDefinition *MD) {
|
|
|
|
if (Previous)
|
|
|
|
Previous->MacroDefinitionRead(PPID, MD);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Dumps deserialized declarations.
|
|
|
|
class DeserializedDeclsDumper : public DelegatingDeserializationListener {
|
|
|
|
public:
|
|
|
|
explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous)
|
|
|
|
: DelegatingDeserializationListener(Previous) { }
|
|
|
|
|
2010-10-15 04:14:18 +08:00
|
|
|
virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
|
|
|
|
llvm::outs() << "PCH DECL: " << D->getDeclKindName();
|
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
2012-02-07 19:57:57 +08:00
|
|
|
llvm::outs() << " - " << *ND;
|
2010-10-15 04:14:18 +08:00
|
|
|
llvm::outs() << "\n";
|
|
|
|
|
2011-10-29 06:54:31 +08:00
|
|
|
DelegatingDeserializationListener::DeclRead(ID, D);
|
2010-10-15 04:14:18 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-10-15 04:14:25 +08:00
|
|
|
/// \brief Checks deserialized declarations and emits error if a name
|
|
|
|
/// matches one given in command-line using -error-on-deserialized-decl.
|
2011-10-29 06:54:31 +08:00
|
|
|
class DeserializedDeclsChecker : public DelegatingDeserializationListener {
|
2010-10-15 04:14:25 +08:00
|
|
|
ASTContext &Ctx;
|
|
|
|
std::set<std::string> NamesToCheck;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DeserializedDeclsChecker(ASTContext &Ctx,
|
|
|
|
const std::set<std::string> &NamesToCheck,
|
|
|
|
ASTDeserializationListener *Previous)
|
2011-10-29 06:54:31 +08:00
|
|
|
: DelegatingDeserializationListener(Previous),
|
|
|
|
Ctx(Ctx), NamesToCheck(NamesToCheck) { }
|
2010-10-15 04:14:25 +08:00
|
|
|
|
|
|
|
virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
|
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
|
|
|
if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
|
|
|
|
unsigned DiagID
|
2011-09-26 07:23:43 +08:00
|
|
|
= Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
|
2010-10-15 04:14:25 +08:00
|
|
|
"%0 was deserialized");
|
|
|
|
Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
|
|
|
|
<< ND->getNameAsString();
|
|
|
|
}
|
|
|
|
|
2011-10-29 06:54:31 +08:00
|
|
|
DelegatingDeserializationListener::DeclRead(ID, D);
|
2010-10-15 04:14:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-10-15 04:14:18 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2009-11-29 17:57:35 +08:00
|
|
|
FrontendAction::FrontendAction() : Instance(0) {}
|
2009-11-14 18:42:35 +08:00
|
|
|
|
|
|
|
FrontendAction::~FrontendAction() {}
|
|
|
|
|
2012-01-21 00:28:04 +08:00
|
|
|
void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
|
|
|
|
ASTUnit *AST) {
|
|
|
|
this->CurrentInput = CurrentInput;
|
2009-11-14 18:42:35 +08:00
|
|
|
CurrentASTUnit.reset(AST);
|
|
|
|
}
|
|
|
|
|
2011-01-26 04:34:14 +08:00
|
|
|
ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef InFile) {
|
2011-01-26 04:34:14 +08:00
|
|
|
ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
|
|
|
|
if (!Consumer)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (CI.getFrontendOpts().AddPluginActions.size() == 0)
|
|
|
|
return Consumer;
|
|
|
|
|
|
|
|
// Make sure the non-plugin consumer is first, so that plugins can't
|
|
|
|
// modifiy the AST.
|
|
|
|
std::vector<ASTConsumer*> Consumers(1, Consumer);
|
|
|
|
|
|
|
|
for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
|
|
|
|
i != e; ++i) {
|
|
|
|
// This is O(|plugins| * |add_plugins|), but since both numbers are
|
|
|
|
// way below 50 in practice, that's ok.
|
|
|
|
for (FrontendPluginRegistry::iterator
|
|
|
|
it = FrontendPluginRegistry::begin(),
|
|
|
|
ie = FrontendPluginRegistry::end();
|
|
|
|
it != ie; ++it) {
|
|
|
|
if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<PluginASTAction> P(it->instantiate());
|
2011-01-26 04:34:14 +08:00
|
|
|
FrontendAction* c = P.get();
|
2011-01-30 05:21:49 +08:00
|
|
|
if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
|
2011-01-26 04:34:14 +08:00
|
|
|
Consumers.push_back(c->CreateASTConsumer(CI, InFile));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MultiplexConsumer(Consumers);
|
|
|
|
}
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
|
2012-01-21 00:28:04 +08:00
|
|
|
const FrontendInputFile &Input) {
|
2009-11-14 18:42:35 +08:00
|
|
|
assert(!Instance && "Already processing a source file!");
|
2012-01-21 00:28:04 +08:00
|
|
|
assert(!Input.File.empty() && "Unexpected empty filename!");
|
|
|
|
setCurrentInput(Input);
|
2009-11-14 18:42:35 +08:00
|
|
|
setCompilerInstance(&CI);
|
|
|
|
|
2011-06-18 08:53:41 +08:00
|
|
|
if (!BeginInvocation(CI))
|
|
|
|
goto failure;
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
// AST files follow a very different path, since they share objects via the
|
|
|
|
// AST unit.
|
2012-01-21 00:28:04 +08:00
|
|
|
if (Input.Kind == IK_AST) {
|
2009-11-14 18:42:35 +08:00
|
|
|
assert(!usesPreprocessorOnly() &&
|
|
|
|
"Attempt to pass AST file to preprocessor only action!");
|
2010-06-08 07:24:43 +08:00
|
|
|
assert(hasASTFileSupport() &&
|
|
|
|
"This action does not have AST file support!");
|
2009-11-14 18:42:35 +08:00
|
|
|
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
|
2009-11-14 18:42:35 +08:00
|
|
|
std::string Error;
|
2012-01-21 00:28:04 +08:00
|
|
|
ASTUnit *AST = ASTUnit::LoadFromASTFile(Input.File, Diags,
|
2010-11-04 06:45:23 +08:00
|
|
|
CI.getFileSystemOpts());
|
2009-12-03 09:45:44 +08:00
|
|
|
if (!AST)
|
2009-11-14 18:42:35 +08:00
|
|
|
goto failure;
|
|
|
|
|
2012-01-21 00:28:04 +08:00
|
|
|
setCurrentInput(Input, AST);
|
2009-11-14 18:42:35 +08:00
|
|
|
|
|
|
|
// Set the shared objects, these are reset when we finish processing the
|
|
|
|
// file, otherwise the CompilerInstance will happily destroy them.
|
|
|
|
CI.setFileManager(&AST->getFileManager());
|
|
|
|
CI.setSourceManager(&AST->getSourceManager());
|
|
|
|
CI.setPreprocessor(&AST->getPreprocessor());
|
|
|
|
CI.setASTContext(&AST->getASTContext());
|
|
|
|
|
|
|
|
// Initialize the action.
|
2012-01-21 00:28:04 +08:00
|
|
|
if (!BeginSourceFileAction(CI, Input.File))
|
2009-11-14 18:42:35 +08:00
|
|
|
goto failure;
|
|
|
|
|
|
|
|
/// Create the AST consumer.
|
2012-01-21 00:28:04 +08:00
|
|
|
CI.setASTConsumer(CreateWrappedASTConsumer(CI, Input.File));
|
2009-11-14 18:42:35 +08:00
|
|
|
if (!CI.hasASTConsumer())
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-08 07:26:47 +08:00
|
|
|
// Set up the file and source managers, if needed.
|
2010-06-08 07:23:50 +08:00
|
|
|
if (!CI.hasFileManager())
|
|
|
|
CI.createFileManager();
|
|
|
|
if (!CI.hasSourceManager())
|
2010-11-23 16:35:12 +08:00
|
|
|
CI.createSourceManager(CI.getFileManager());
|
2010-06-08 07:26:47 +08:00
|
|
|
|
|
|
|
// IR files bypass the rest of initialization.
|
2012-01-21 00:28:04 +08:00
|
|
|
if (Input.Kind == IK_LLVM_IR) {
|
2010-06-08 07:26:47 +08:00
|
|
|
assert(hasIRSupport() &&
|
|
|
|
"This action does not have IR file support!");
|
|
|
|
|
|
|
|
// Inform the diagnostic client we are processing a source file.
|
|
|
|
CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
|
|
|
|
|
|
|
|
// Initialize the action.
|
2012-01-21 00:28:04 +08:00
|
|
|
if (!BeginSourceFileAction(CI, Input.File))
|
2010-06-08 07:26:47 +08:00
|
|
|
goto failure;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the preprocessor.
|
2010-06-08 07:23:50 +08:00
|
|
|
CI.createPreprocessor();
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
// Inform the diagnostic client we are processing a source file.
|
|
|
|
CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
|
|
|
|
&CI.getPreprocessor());
|
|
|
|
|
|
|
|
// Initialize the action.
|
2012-01-21 00:28:04 +08:00
|
|
|
if (!BeginSourceFileAction(CI, Input.File))
|
2009-11-14 18:42:35 +08:00
|
|
|
goto failure;
|
|
|
|
|
|
|
|
/// Create the AST context and consumer unless this is a preprocessor only
|
|
|
|
/// action.
|
|
|
|
if (!usesPreprocessorOnly()) {
|
|
|
|
CI.createASTContext();
|
|
|
|
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ASTConsumer> Consumer(
|
2012-01-21 00:28:04 +08:00
|
|
|
CreateWrappedASTConsumer(CI, Input.File));
|
2010-10-30 03:49:13 +08:00
|
|
|
if (!Consumer)
|
|
|
|
goto failure;
|
2010-07-30 08:29:29 +08:00
|
|
|
|
2010-10-25 01:26:36 +08:00
|
|
|
CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
|
|
|
|
|
2011-03-10 01:21:42 +08:00
|
|
|
if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
|
|
|
|
// Convert headers to PCH and chain them.
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ExternalASTSource> source;
|
2011-03-10 01:21:42 +08:00
|
|
|
source.reset(ChainedIncludesSource::create(CI));
|
|
|
|
if (!source)
|
|
|
|
goto failure;
|
|
|
|
CI.getASTContext().setExternalSource(source);
|
|
|
|
|
|
|
|
} else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
|
|
|
|
// Use PCH.
|
2009-11-14 18:42:35 +08:00
|
|
|
assert(hasPCHSupport() && "This action does not have PCH support!");
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
ASTDeserializationListener *DeserialListener =
|
|
|
|
Consumer->GetASTDeserializationListener();
|
2010-10-15 04:14:18 +08:00
|
|
|
if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
|
|
|
|
DeserialListener = new DeserializedDeclsDumper(DeserialListener);
|
2010-10-15 04:14:25 +08:00
|
|
|
if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
|
|
|
|
DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
|
|
|
|
CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
|
|
|
|
DeserialListener);
|
2009-11-14 18:42:35 +08:00
|
|
|
CI.createPCHExternalASTSource(
|
2010-07-27 08:27:13 +08:00
|
|
|
CI.getPreprocessorOpts().ImplicitPCHInclude,
|
2010-07-30 08:29:29 +08:00
|
|
|
CI.getPreprocessorOpts().DisablePCHValidation,
|
2011-02-06 03:42:43 +08:00
|
|
|
CI.getPreprocessorOpts().DisableStatCache,
|
2012-03-07 09:51:17 +08:00
|
|
|
CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
|
2010-10-15 04:14:18 +08:00
|
|
|
DeserialListener);
|
2009-11-14 18:42:35 +08:00
|
|
|
if (!CI.getASTContext().getExternalSource())
|
|
|
|
goto failure;
|
|
|
|
}
|
2010-07-10 05:00:24 +08:00
|
|
|
|
2010-07-30 08:29:29 +08:00
|
|
|
CI.setASTConsumer(Consumer.take());
|
2010-07-10 05:00:24 +08:00
|
|
|
if (!CI.hasASTConsumer())
|
|
|
|
goto failure;
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
2011-08-06 06:17:03 +08:00
|
|
|
// Initialize built-in info as long as we aren't using an external AST
|
2009-11-14 18:42:35 +08:00
|
|
|
// source.
|
|
|
|
if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
|
|
|
|
Preprocessor &PP = CI.getPreprocessor();
|
|
|
|
PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
|
2012-03-11 15:00:24 +08:00
|
|
|
PP.getLangOpts());
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
// If there is a layout overrides file, attach an external AST source that
|
|
|
|
// provides the layouts from that file.
|
|
|
|
if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
|
|
|
|
CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
|
2012-02-05 10:12:40 +08:00
|
|
|
OwningPtr<ExternalASTSource>
|
Extend the ExternalASTSource interface to allow the AST source to
provide the layout of records, rather than letting Clang compute
the layout itself. LLDB provides the motivation for this feature:
because various layout-altering attributes (packed, aligned, etc.)
don't get reliably get placed into DWARF, the record layouts computed
by LLDB from the reconstructed records differ from the actual layouts,
and badness occurs. This interface lets the DWARF data drive layout,
so we don't need the attributes preserved to get the answer write.
The testing methodology for this change is fun. I've introduced a
variant of -fdump-record-layouts called -fdump-record-layouts-simple
that always has the simple C format and provides size/alignment/field
offsets. There is also a -cc1 option -foverride-record-layout=<file>
to take the output of -fdump-record-layouts-simple and parse it to
produce a set of overridden layouts, which is introduced into the AST
via a testing-only ExternalASTSource (called
LayoutOverrideSource). Each test contains a number of records to lay
out, which use various layout-changing attributes, and then dumps the
layouts. We then run the test again, using the preprocessor to
eliminate the layout-changing attributes entirely (which would give us
different layouts for the records), but supplying the
previously-computed record layouts. Finally, we diff the layouts
produced from the two runs to be sure that they are identical.
Note that this code makes the assumption that we don't *have* to
provide the offsets of bases or virtual bases to get the layout right,
because the alignment attributes don't affect it. I believe this
assumption holds, but if it does not, we can extend
LayoutOverrideSource to also provide base offset information.
Fixes the Clang side of <rdar://problem/10169539>.
llvm-svn: 149055
2012-01-26 15:55:45 +08:00
|
|
|
Override(new LayoutOverrideSource(
|
|
|
|
CI.getFrontendOpts().OverrideRecordLayoutsFile));
|
|
|
|
CI.getASTContext().setExternalSource(Override);
|
|
|
|
}
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// If we failed, reset state since the client will not end up calling the
|
|
|
|
// matching EndSourceFile().
|
|
|
|
failure:
|
|
|
|
if (isCurrentFileAST()) {
|
2011-03-22 02:40:17 +08:00
|
|
|
CI.setASTContext(0);
|
|
|
|
CI.setPreprocessor(0);
|
|
|
|
CI.setSourceManager(0);
|
|
|
|
CI.setFileManager(0);
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CI.getDiagnosticClient().EndSourceFile();
|
2012-01-21 00:28:04 +08:00
|
|
|
setCurrentInput(FrontendInputFile());
|
2009-11-14 18:42:35 +08:00
|
|
|
setCompilerInstance(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrontendAction::Execute() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
|
|
|
|
// Initialize the main file entry. This needs to be delayed until after PCH
|
|
|
|
// has loaded.
|
2012-01-06 05:36:25 +08:00
|
|
|
if (!isCurrentFileAST()) {
|
2012-01-28 03:52:33 +08:00
|
|
|
if (!CI.InitializeSourceManager(getCurrentFile(),
|
|
|
|
getCurrentInput().IsSystem
|
|
|
|
? SrcMgr::C_System
|
|
|
|
: SrcMgr::C_User))
|
2009-11-14 18:42:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-29 17:57:35 +08:00
|
|
|
if (CI.hasFrontendTimer()) {
|
|
|
|
llvm::TimeRegion Timer(CI.getFrontendTimer());
|
|
|
|
ExecuteAction();
|
|
|
|
}
|
|
|
|
else ExecuteAction();
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrontendAction::EndSourceFile() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
|
2011-02-10 02:47:31 +08:00
|
|
|
// Inform the diagnostic client we are done with this source file.
|
|
|
|
CI.getDiagnosticClient().EndSourceFile();
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
// Finalize the action.
|
|
|
|
EndSourceFileAction();
|
|
|
|
|
|
|
|
// Release the consumer and the AST, in that order since the consumer may
|
|
|
|
// perform actions in its destructor which require the context.
|
|
|
|
//
|
|
|
|
// FIXME: There is more per-file stuff we could just drop here?
|
|
|
|
if (CI.getFrontendOpts().DisableFree) {
|
|
|
|
CI.takeASTConsumer();
|
2010-08-13 07:31:19 +08:00
|
|
|
if (!isCurrentFileAST()) {
|
|
|
|
CI.takeSema();
|
2011-03-22 02:40:17 +08:00
|
|
|
CI.resetAndLeakASTContext();
|
2010-08-13 07:31:19 +08:00
|
|
|
}
|
2009-11-14 18:42:35 +08:00
|
|
|
} else {
|
2010-08-13 07:31:19 +08:00
|
|
|
if (!isCurrentFileAST()) {
|
|
|
|
CI.setSema(0);
|
2009-11-14 18:42:35 +08:00
|
|
|
CI.setASTContext(0);
|
2010-08-13 07:31:19 +08:00
|
|
|
}
|
|
|
|
CI.setASTConsumer(0);
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
2010-03-23 13:09:10 +08:00
|
|
|
// Inform the preprocessor we are done.
|
|
|
|
if (CI.hasPreprocessor())
|
|
|
|
CI.getPreprocessor().EndSourceFile();
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
if (CI.getFrontendOpts().ShowStats) {
|
|
|
|
llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
|
|
|
|
CI.getPreprocessor().PrintStats();
|
|
|
|
CI.getPreprocessor().getIdentifierTable().PrintStats();
|
|
|
|
CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
|
|
|
|
CI.getSourceManager().PrintStats();
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup the output streams, and erase the output files if we encountered
|
|
|
|
// an error.
|
2010-11-19 05:47:07 +08:00
|
|
|
CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
|
2009-11-14 18:42:35 +08:00
|
|
|
|
|
|
|
if (isCurrentFileAST()) {
|
2010-08-13 07:31:19 +08:00
|
|
|
CI.takeSema();
|
2011-03-22 02:40:17 +08:00
|
|
|
CI.resetAndLeakASTContext();
|
|
|
|
CI.resetAndLeakPreprocessor();
|
|
|
|
CI.resetAndLeakSourceManager();
|
|
|
|
CI.resetAndLeakFileManager();
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setCompilerInstance(0);
|
2012-01-21 00:28:04 +08:00
|
|
|
setCurrentInput(FrontendInputFile());
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility Actions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ASTFrontendAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
|
|
|
|
// FIXME: Move the truncation aspect of this into Sema, we delayed this till
|
|
|
|
// here so the source manager would be initialized.
|
|
|
|
if (hasCodeCompletionSupport() &&
|
|
|
|
!CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
|
|
|
|
CI.createCodeCompletionConsumer();
|
|
|
|
|
|
|
|
// Use a code completion consumer?
|
|
|
|
CodeCompleteConsumer *CompletionConsumer = 0;
|
|
|
|
if (CI.hasCodeCompletionConsumer())
|
|
|
|
CompletionConsumer = &CI.getCodeCompletionConsumer();
|
|
|
|
|
2010-08-13 07:31:19 +08:00
|
|
|
if (!CI.hasSema())
|
2011-08-26 06:30:56 +08:00
|
|
|
CI.createSema(getTranslationUnitKind(), CompletionConsumer);
|
2010-08-13 07:31:19 +08:00
|
|
|
|
2012-04-12 18:11:59 +08:00
|
|
|
ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
|
|
|
|
CI.getFrontendOpts().SkipFunctionBodies);
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void PluginASTAction::anchor() { }
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
ASTConsumer *
|
|
|
|
PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef InFile) {
|
2009-12-12 13:05:38 +08:00
|
|
|
llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
2011-06-17 00:17:05 +08:00
|
|
|
|
|
|
|
ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef InFile) {
|
2011-06-17 00:17:05 +08:00
|
|
|
return WrappedAction->CreateASTConsumer(CI, InFile);
|
|
|
|
}
|
2011-06-18 08:53:41 +08:00
|
|
|
bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
|
|
|
|
return WrappedAction->BeginInvocation(CI);
|
|
|
|
}
|
2011-06-17 00:17:05 +08:00
|
|
|
bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Filename) {
|
2012-01-21 00:28:04 +08:00
|
|
|
WrappedAction->setCurrentInput(getCurrentInput());
|
2011-06-18 08:53:41 +08:00
|
|
|
WrappedAction->setCompilerInstance(&CI);
|
2011-06-17 00:17:05 +08:00
|
|
|
return WrappedAction->BeginSourceFileAction(CI, Filename);
|
|
|
|
}
|
|
|
|
void WrapperFrontendAction::ExecuteAction() {
|
|
|
|
WrappedAction->ExecuteAction();
|
|
|
|
}
|
|
|
|
void WrapperFrontendAction::EndSourceFileAction() {
|
|
|
|
WrappedAction->EndSourceFileAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapperFrontendAction::usesPreprocessorOnly() const {
|
|
|
|
return WrappedAction->usesPreprocessorOnly();
|
|
|
|
}
|
2011-08-26 06:30:56 +08:00
|
|
|
TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
|
|
|
|
return WrappedAction->getTranslationUnitKind();
|
2011-06-17 00:17:05 +08:00
|
|
|
}
|
|
|
|
bool WrapperFrontendAction::hasPCHSupport() const {
|
|
|
|
return WrappedAction->hasPCHSupport();
|
|
|
|
}
|
|
|
|
bool WrapperFrontendAction::hasASTFileSupport() const {
|
|
|
|
return WrappedAction->hasASTFileSupport();
|
|
|
|
}
|
|
|
|
bool WrapperFrontendAction::hasIRSupport() const {
|
|
|
|
return WrappedAction->hasIRSupport();
|
|
|
|
}
|
|
|
|
bool WrapperFrontendAction::hasCodeCompletionSupport() const {
|
|
|
|
return WrappedAction->hasCodeCompletionSupport();
|
|
|
|
}
|
|
|
|
|
|
|
|
WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
|
|
|
|
: WrappedAction(WrappedAction) {}
|
|
|
|
|