2009-11-14 18:42:35 +08:00
|
|
|
//===--- FrontendAction.cpp -----------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-11-14 18:42:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
2019-11-16 08:36:00 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2019-08-05 21:59:26 +08:00
|
|
|
#include "clang/Basic/LangStandard.h"
|
2009-11-14 18:42:35 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#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"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2017-03-08 04:20:23 +08:00
|
|
|
#include "clang/Lex/LiteralSupport.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.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"
|
2013-01-24 06:38:11 +08:00
|
|
|
#include "clang/Serialization/GlobalModuleIndex.h"
|
2021-09-16 09:42:12 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2018-11-18 02:04:13 +08:00
|
|
|
#include "llvm/Support/BuryPointer.h"
|
2009-11-14 18:42:35 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-10-23 14:18:24 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2009-11-14 18:42:35 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-06-13 01:19:42 +08:00
|
|
|
#include <system_error>
|
2009-11-14 18:42:35 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2016-08-05 19:01:08 +08:00
|
|
|
LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
|
2014-07-11 23:06:24 +08:00
|
|
|
|
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;
|
2014-05-08 12:26:47 +08:00
|
|
|
bool DeletePrevious;
|
2010-10-15 04:14:18 +08:00
|
|
|
|
|
|
|
public:
|
2011-10-29 06:54:31 +08:00
|
|
|
explicit DelegatingDeserializationListener(
|
2014-05-08 12:26:47 +08:00
|
|
|
ASTDeserializationListener *Previous, bool DeletePrevious)
|
|
|
|
: Previous(Previous), DeletePrevious(DeletePrevious) {}
|
2015-04-11 10:00:23 +08:00
|
|
|
~DelegatingDeserializationListener() override {
|
2014-05-08 12:26:47 +08:00
|
|
|
if (DeletePrevious)
|
|
|
|
delete Previous;
|
|
|
|
}
|
2010-10-15 04:14:18 +08:00
|
|
|
|
2014-03-13 14:07:04 +08:00
|
|
|
void ReaderInitialized(ASTReader *Reader) override {
|
2011-10-29 06:54:31 +08:00
|
|
|
if (Previous)
|
|
|
|
Previous->ReaderInitialized(Reader);
|
|
|
|
}
|
2014-03-13 14:07:04 +08:00
|
|
|
void IdentifierRead(serialization::IdentID ID,
|
|
|
|
IdentifierInfo *II) override {
|
2011-10-29 06:54:31 +08:00
|
|
|
if (Previous)
|
|
|
|
Previous->IdentifierRead(ID, II);
|
|
|
|
}
|
2014-03-13 14:07:04 +08:00
|
|
|
void TypeRead(serialization::TypeIdx Idx, QualType T) override {
|
2011-10-29 06:54:31 +08:00
|
|
|
if (Previous)
|
|
|
|
Previous->TypeRead(Idx, T);
|
|
|
|
}
|
2014-03-13 14:07:04 +08:00
|
|
|
void DeclRead(serialization::DeclID ID, const Decl *D) override {
|
2011-10-29 06:54:31 +08:00
|
|
|
if (Previous)
|
|
|
|
Previous->DeclRead(ID, D);
|
|
|
|
}
|
2014-03-13 14:07:04 +08:00
|
|
|
void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
|
2011-10-29 06:54:31 +08:00
|
|
|
if (Previous)
|
|
|
|
Previous->SelectorRead(ID, Sel);
|
|
|
|
}
|
2014-03-13 14:07:04 +08:00
|
|
|
void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
|
2015-05-04 10:25:31 +08:00
|
|
|
MacroDefinitionRecord *MD) override {
|
2011-10-29 06:54:31 +08:00
|
|
|
if (Previous)
|
|
|
|
Previous->MacroDefinitionRead(PPID, MD);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Dumps deserialized declarations.
|
2011-10-29 06:54:31 +08:00
|
|
|
class DeserializedDeclsDumper : public DelegatingDeserializationListener {
|
|
|
|
public:
|
2014-05-08 12:26:47 +08:00
|
|
|
explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
|
|
|
|
bool DeletePrevious)
|
|
|
|
: DelegatingDeserializationListener(Previous, DeletePrevious) {}
|
2011-10-29 06:54:31 +08:00
|
|
|
|
2014-03-13 14:07:04 +08:00
|
|
|
void DeclRead(serialization::DeclID ID, const Decl *D) override {
|
2010-10-15 04:14:18 +08:00
|
|
|
llvm::outs() << "PCH DECL: " << D->getDeclKindName();
|
2018-05-20 17:38:52 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
|
|
|
|
llvm::outs() << " - ";
|
|
|
|
ND->printQualifiedName(llvm::outs());
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Checks deserialized declarations and emits error if a name
|
2012-05-30 01:05:42 +08:00
|
|
|
/// matches one given in command-line using -error-on-deserialized-decl.
|
|
|
|
class DeserializedDeclsChecker : public DelegatingDeserializationListener {
|
|
|
|
ASTContext &Ctx;
|
|
|
|
std::set<std::string> NamesToCheck;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DeserializedDeclsChecker(ASTContext &Ctx,
|
|
|
|
const std::set<std::string> &NamesToCheck,
|
2014-05-08 12:26:47 +08:00
|
|
|
ASTDeserializationListener *Previous,
|
|
|
|
bool DeletePrevious)
|
|
|
|
: DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
|
|
|
|
NamesToCheck(NamesToCheck) {}
|
2012-05-30 01:05:42 +08:00
|
|
|
|
2014-03-13 14:07:04 +08:00
|
|
|
void DeclRead(serialization::DeclID ID, const Decl *D) override {
|
2012-05-30 01:05:42 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
|
|
|
if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
|
|
|
|
unsigned DiagID
|
|
|
|
= Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
|
|
|
|
"%0 was deserialized");
|
|
|
|
Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
|
2020-08-05 18:48:09 +08:00
|
|
|
<< ND;
|
2012-05-30 01:05:42 +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
|
|
|
|
|
2014-05-22 12:46:25 +08:00
|
|
|
FrontendAction::FrontendAction() : Instance(nullptr) {}
|
2009-11-14 18:42:35 +08:00
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
FrontendAction::~FrontendAction() {}
|
2009-11-14 18:42:35 +08:00
|
|
|
|
2012-01-21 00:28:04 +08:00
|
|
|
void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
|
2014-08-11 01:03:42 +08:00
|
|
|
std::unique_ptr<ASTUnit> AST) {
|
2012-01-21 00:28:04 +08:00
|
|
|
this->CurrentInput = CurrentInput;
|
2014-08-11 01:03:42 +08:00
|
|
|
CurrentASTUnit = std::move(AST);
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
2017-05-06 06:18:51 +08:00
|
|
|
Module *FrontendAction::getCurrentModule() const {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
return CI.getPreprocessor().getHeaderSearchInfo().lookupModule(
|
2021-10-12 15:04:37 +08:00
|
|
|
CI.getLangOpts().CurrentModule, SourceLocation(), /*AllowSearch*/false);
|
2017-05-06 06:18:51 +08:00
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) {
|
|
|
|
std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
|
2011-01-26 04:34:14 +08:00
|
|
|
if (!Consumer)
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
2011-01-26 04:34:14 +08:00
|
|
|
|
2019-01-05 09:10:20 +08:00
|
|
|
// Validate -add-plugin args.
|
|
|
|
bool FoundAllPlugins = true;
|
|
|
|
for (const std::string &Arg : CI.getFrontendOpts().AddPluginActions) {
|
|
|
|
bool Found = false;
|
2020-06-19 07:40:00 +08:00
|
|
|
for (const FrontendPluginRegistry::entry &Plugin :
|
|
|
|
FrontendPluginRegistry::entries()) {
|
|
|
|
if (Plugin.getName() == Arg)
|
2019-01-05 09:10:20 +08:00
|
|
|
Found = true;
|
|
|
|
}
|
|
|
|
if (!Found) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) << Arg;
|
|
|
|
FoundAllPlugins = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!FoundAllPlugins)
|
|
|
|
return nullptr;
|
|
|
|
|
2019-01-05 09:19:14 +08:00
|
|
|
// If there are no registered plugins we don't need to wrap the consumer
|
|
|
|
if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
|
|
|
|
return Consumer;
|
|
|
|
|
2018-05-17 17:21:07 +08:00
|
|
|
// If this is a code completion run, avoid invoking the plugin consumers
|
|
|
|
if (CI.hasCodeCompletionConsumer())
|
|
|
|
return Consumer;
|
|
|
|
|
2018-05-17 17:15:22 +08:00
|
|
|
// Collect the list of plugins that go before the main action (in Consumers)
|
|
|
|
// or after it (in AfterConsumers)
|
|
|
|
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
|
2016-03-15 20:51:40 +08:00
|
|
|
std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
|
2020-06-19 07:40:00 +08:00
|
|
|
for (const FrontendPluginRegistry::entry &Plugin :
|
|
|
|
FrontendPluginRegistry::entries()) {
|
|
|
|
std::unique_ptr<PluginASTAction> P = Plugin.instantiate();
|
2016-03-15 20:51:40 +08:00
|
|
|
PluginASTAction::ActionType ActionType = P->getActionType();
|
2021-10-20 05:50:44 +08:00
|
|
|
if (ActionType == PluginASTAction::CmdlineAfterMainAction ||
|
|
|
|
ActionType == PluginASTAction::CmdlineBeforeMainAction) {
|
2016-03-15 20:51:40 +08:00
|
|
|
// This is O(|plugins| * |add_plugins|), but since both numbers are
|
|
|
|
// way below 50 in practice, that's ok.
|
2020-06-19 07:40:00 +08:00
|
|
|
if (llvm::any_of(CI.getFrontendOpts().AddPluginActions,
|
|
|
|
[&](const std::string &PluginAction) {
|
|
|
|
return PluginAction == Plugin.getName();
|
2021-10-20 05:50:44 +08:00
|
|
|
})) {
|
|
|
|
if (ActionType == PluginASTAction::CmdlineBeforeMainAction)
|
|
|
|
ActionType = PluginASTAction::AddBeforeMainAction;
|
|
|
|
else
|
|
|
|
ActionType = PluginASTAction::AddAfterMainAction;
|
|
|
|
}
|
2016-03-15 20:51:40 +08:00
|
|
|
}
|
|
|
|
if ((ActionType == PluginASTAction::AddBeforeMainAction ||
|
|
|
|
ActionType == PluginASTAction::AddAfterMainAction) &&
|
2020-01-29 03:23:46 +08:00
|
|
|
P->ParseArgs(
|
2020-06-19 07:40:00 +08:00
|
|
|
CI,
|
|
|
|
CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())])) {
|
2016-03-15 20:51:40 +08:00
|
|
|
std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
|
|
|
|
if (ActionType == PluginASTAction::AddBeforeMainAction) {
|
|
|
|
Consumers.push_back(std::move(PluginConsumer));
|
|
|
|
} else {
|
|
|
|
AfterConsumers.push_back(std::move(PluginConsumer));
|
|
|
|
}
|
2011-01-26 04:34:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-15 20:51:40 +08:00
|
|
|
// Add to Consumers the main consumer, then all the plugins that go after it
|
|
|
|
Consumers.push_back(std::move(Consumer));
|
2021-10-21 06:43:10 +08:00
|
|
|
if (!AfterConsumers.empty()) {
|
|
|
|
// If we have plugins after the main consumer, which may be the codegen
|
|
|
|
// action, they likely will need the ASTContext, so don't clear it in the
|
|
|
|
// codegen action.
|
|
|
|
CI.getCodeGenOpts().ClearASTBeforeBackend = false;
|
|
|
|
for (auto &C : AfterConsumers)
|
|
|
|
Consumers.push_back(std::move(C));
|
2016-03-15 20:51:40 +08:00
|
|
|
}
|
|
|
|
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<MultiplexConsumer>(std::move(Consumers));
|
2011-01-26 04:34:14 +08:00
|
|
|
}
|
|
|
|
|
2017-05-06 06:18:51 +08:00
|
|
|
/// For preprocessed files, if the first line is the linemarker and specifies
|
|
|
|
/// the original source file name, use that name as the input file name.
|
|
|
|
/// Returns the location of the first token after the line marker directive.
|
|
|
|
///
|
|
|
|
/// \param CI The compiler instance.
|
|
|
|
/// \param InputFile Populated with the filename from the line marker.
|
2017-06-29 10:19:42 +08:00
|
|
|
/// \param IsModuleMap If \c true, add a line note corresponding to this line
|
|
|
|
/// directive. (We need to do this because the directive will not be
|
|
|
|
/// visited by the preprocessor.)
|
2017-05-06 06:18:51 +08:00
|
|
|
static SourceLocation ReadOriginalFileName(CompilerInstance &CI,
|
|
|
|
std::string &InputFile,
|
2017-06-29 10:19:42 +08:00
|
|
|
bool IsModuleMap = false) {
|
2017-03-08 04:20:23 +08:00
|
|
|
auto &SourceMgr = CI.getSourceManager();
|
|
|
|
auto MainFileID = SourceMgr.getMainFileID();
|
2017-05-06 06:18:51 +08:00
|
|
|
|
2020-10-14 23:11:09 +08:00
|
|
|
auto MainFileBuf = SourceMgr.getBufferOrNone(MainFileID);
|
|
|
|
if (!MainFileBuf)
|
2017-05-06 06:18:51 +08:00
|
|
|
return SourceLocation();
|
2017-03-08 04:20:23 +08:00
|
|
|
|
|
|
|
std::unique_ptr<Lexer> RawLexer(
|
2020-10-14 23:11:09 +08:00
|
|
|
new Lexer(MainFileID, *MainFileBuf, SourceMgr, CI.getLangOpts()));
|
2017-03-08 04:20:23 +08:00
|
|
|
|
|
|
|
// If the first line has the syntax of
|
|
|
|
//
|
|
|
|
// # NUM "FILENAME"
|
|
|
|
//
|
|
|
|
// we use FILENAME as the input file name.
|
|
|
|
Token T;
|
|
|
|
if (RawLexer->LexFromRawLexer(T) || T.getKind() != tok::hash)
|
2017-05-06 06:18:51 +08:00
|
|
|
return SourceLocation();
|
2017-03-08 04:20:23 +08:00
|
|
|
if (RawLexer->LexFromRawLexer(T) || T.isAtStartOfLine() ||
|
|
|
|
T.getKind() != tok::numeric_constant)
|
2017-05-06 06:18:51 +08:00
|
|
|
return SourceLocation();
|
|
|
|
|
|
|
|
unsigned LineNo;
|
|
|
|
SourceLocation LineNoLoc = T.getLocation();
|
2017-06-29 10:19:42 +08:00
|
|
|
if (IsModuleMap) {
|
2017-05-06 06:18:51 +08:00
|
|
|
llvm::SmallString<16> Buffer;
|
|
|
|
if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts())
|
|
|
|
.getAsInteger(10, LineNo))
|
|
|
|
return SourceLocation();
|
|
|
|
}
|
|
|
|
|
2017-03-08 04:20:23 +08:00
|
|
|
RawLexer->LexFromRawLexer(T);
|
|
|
|
if (T.isAtStartOfLine() || T.getKind() != tok::string_literal)
|
2017-05-06 06:18:51 +08:00
|
|
|
return SourceLocation();
|
2017-03-08 04:20:23 +08:00
|
|
|
|
|
|
|
StringLiteralParser Literal(T, CI.getPreprocessor());
|
|
|
|
if (Literal.hadError)
|
2017-05-06 06:18:51 +08:00
|
|
|
return SourceLocation();
|
|
|
|
RawLexer->LexFromRawLexer(T);
|
|
|
|
if (T.isNot(tok::eof) && !T.isAtStartOfLine())
|
|
|
|
return SourceLocation();
|
2017-03-08 04:20:23 +08:00
|
|
|
InputFile = Literal.GetString().str();
|
2017-05-06 06:18:51 +08:00
|
|
|
|
2017-06-29 10:19:42 +08:00
|
|
|
if (IsModuleMap)
|
2017-05-06 06:18:51 +08:00
|
|
|
CI.getSourceManager().AddLineNote(
|
2017-05-23 05:42:58 +08:00
|
|
|
LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false,
|
2017-06-29 10:19:42 +08:00
|
|
|
false, SrcMgr::C_User_ModuleMap);
|
2017-05-06 06:18:51 +08:00
|
|
|
|
|
|
|
return T.getLocation();
|
2017-03-08 04:20:23 +08:00
|
|
|
}
|
|
|
|
|
2017-04-28 09:49:42 +08:00
|
|
|
static SmallVectorImpl<char> &
|
|
|
|
operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
|
|
|
|
Includes.append(RHS.begin(), RHS.end());
|
|
|
|
return Includes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addHeaderInclude(StringRef HeaderName,
|
|
|
|
SmallVectorImpl<char> &Includes,
|
|
|
|
const LangOptions &LangOpts,
|
|
|
|
bool IsExternC) {
|
|
|
|
if (IsExternC && LangOpts.CPlusPlus)
|
|
|
|
Includes += "extern \"C\" {\n";
|
2018-10-31 04:31:30 +08:00
|
|
|
if (LangOpts.ObjC)
|
2017-04-28 09:49:42 +08:00
|
|
|
Includes += "#import \"";
|
|
|
|
else
|
|
|
|
Includes += "#include \"";
|
|
|
|
|
|
|
|
Includes += HeaderName;
|
|
|
|
|
|
|
|
Includes += "\"\n";
|
|
|
|
if (IsExternC && LangOpts.CPlusPlus)
|
|
|
|
Includes += "}\n";
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
/// Collect the set of header includes needed to construct the given
|
2017-04-28 09:49:42 +08:00
|
|
|
/// module and update the TopHeaders file set of the module.
|
|
|
|
///
|
|
|
|
/// \param Module The module we're collecting includes from.
|
|
|
|
///
|
|
|
|
/// \param Includes Will be augmented with the set of \#includes or \#imports
|
|
|
|
/// needed to load all of the named headers.
|
2017-06-02 09:55:39 +08:00
|
|
|
static std::error_code collectModuleHeaderIncludes(
|
|
|
|
const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag,
|
|
|
|
ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) {
|
2017-04-28 09:49:42 +08:00
|
|
|
// Don't collect any headers for unavailable modules.
|
|
|
|
if (!Module->isAvailable())
|
|
|
|
return std::error_code();
|
|
|
|
|
2017-06-02 09:55:39 +08:00
|
|
|
// Resolve all lazy header directives to header files.
|
2022-02-26 00:21:32 +08:00
|
|
|
ModMap.resolveHeaderDirectives(Module, /*File=*/llvm::None);
|
2017-06-02 09:55:39 +08:00
|
|
|
|
|
|
|
// If any headers are missing, we can't build this module. In most cases,
|
|
|
|
// diagnostics for this should have already been produced; we only get here
|
|
|
|
// if explicit stat information was provided.
|
|
|
|
// FIXME: If the name resolves to a file with different stat information,
|
|
|
|
// produce a better diagnostic.
|
|
|
|
if (!Module->MissingHeaders.empty()) {
|
|
|
|
auto &MissingHeader = Module->MissingHeaders.front();
|
|
|
|
Diag.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
|
|
|
|
<< MissingHeader.IsUmbrella << MissingHeader.FileName;
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2017-04-28 09:49:42 +08:00
|
|
|
// Add includes for each of these headers.
|
|
|
|
for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
|
|
|
|
for (Module::Header &H : Module->Headers[HK]) {
|
|
|
|
Module->addTopHeader(H.Entry);
|
|
|
|
// Use the path as specified in the module map file. We'll look for this
|
|
|
|
// file relative to the module build directory (the directory containing
|
|
|
|
// the module map file) so this will find the same file that we found
|
|
|
|
// while parsing the module map.
|
2021-05-17 15:40:29 +08:00
|
|
|
addHeaderInclude(H.PathRelativeToRootModuleDirectory, Includes, LangOpts,
|
|
|
|
Module->IsExternC);
|
2017-04-28 09:49:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Note that Module->PrivateHeaders will not be a TopHeader.
|
|
|
|
|
|
|
|
if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
|
|
|
|
Module->addTopHeader(UmbrellaHeader.Entry);
|
|
|
|
if (Module->Parent)
|
|
|
|
// Include the umbrella header for submodules.
|
2021-05-17 15:40:29 +08:00
|
|
|
addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
|
|
|
|
Includes, LangOpts, Module->IsExternC);
|
2017-04-28 09:49:42 +08:00
|
|
|
} else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
|
|
|
|
// Add all of the headers we find in this subdirectory.
|
|
|
|
std::error_code EC;
|
|
|
|
SmallString<128> DirNative;
|
|
|
|
llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
|
|
|
|
|
2019-03-27 06:32:06 +08:00
|
|
|
llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
|
2020-04-22 06:30:54 +08:00
|
|
|
SmallVector<std::pair<std::string, const FileEntry *>, 8> Headers;
|
2018-10-10 21:27:25 +08:00
|
|
|
for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
|
2017-04-28 09:49:42 +08:00
|
|
|
Dir != End && !EC; Dir.increment(EC)) {
|
2018-07-31 03:24:48 +08:00
|
|
|
// Check whether this entry has an extension typically associated with
|
2017-04-28 09:49:42 +08:00
|
|
|
// headers.
|
2018-09-14 20:47:38 +08:00
|
|
|
if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
|
|
|
|
.Cases(".h", ".H", ".hh", ".hpp", true)
|
|
|
|
.Default(false))
|
2017-04-28 09:49:42 +08:00
|
|
|
continue;
|
|
|
|
|
2019-08-02 05:31:56 +08:00
|
|
|
auto Header = FileMgr.getFile(Dir->path());
|
2017-04-28 09:49:42 +08:00
|
|
|
// FIXME: This shouldn't happen unless there is a file system race. Is
|
|
|
|
// that worth diagnosing?
|
|
|
|
if (!Header)
|
|
|
|
continue;
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
// If this header is marked 'unavailable' in this module, don't include
|
2017-04-28 09:49:42 +08:00
|
|
|
// it.
|
2019-08-02 05:31:56 +08:00
|
|
|
if (ModMap.isHeaderUnavailableInModule(*Header, Module))
|
2017-04-28 09:49:42 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Compute the relative path from the directory to this file.
|
|
|
|
SmallVector<StringRef, 16> Components;
|
2018-09-14 20:47:38 +08:00
|
|
|
auto PathIt = llvm::sys::path::rbegin(Dir->path());
|
2017-04-28 09:49:42 +08:00
|
|
|
for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
|
|
|
|
Components.push_back(*PathIt);
|
2021-05-17 15:40:29 +08:00
|
|
|
SmallString<128> RelativeHeader(
|
|
|
|
UmbrellaDir.PathRelativeToRootModuleDirectory);
|
2017-04-28 09:49:42 +08:00
|
|
|
for (auto It = Components.rbegin(), End = Components.rend(); It != End;
|
|
|
|
++It)
|
|
|
|
llvm::sys::path::append(RelativeHeader, *It);
|
|
|
|
|
2020-04-22 06:30:54 +08:00
|
|
|
std::string RelName = RelativeHeader.c_str();
|
|
|
|
Headers.push_back(std::make_pair(RelName, *Header));
|
2017-04-28 09:49:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
2020-04-22 06:30:54 +08:00
|
|
|
|
|
|
|
// Sort header paths and make the header inclusion order deterministic
|
|
|
|
// across different OSs and filesystems.
|
|
|
|
llvm::sort(Headers.begin(), Headers.end(), [](
|
|
|
|
const std::pair<std::string, const FileEntry *> &LHS,
|
|
|
|
const std::pair<std::string, const FileEntry *> &RHS) {
|
|
|
|
return LHS.first < RHS.first;
|
|
|
|
});
|
|
|
|
for (auto &H : Headers) {
|
|
|
|
// Include this header as part of the umbrella directory.
|
|
|
|
Module->addTopHeader(H.second);
|
|
|
|
addHeaderInclude(H.first, Includes, LangOpts, Module->IsExternC);
|
|
|
|
}
|
2017-04-28 09:49:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse into submodules.
|
|
|
|
for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
|
|
|
|
SubEnd = Module->submodule_end();
|
|
|
|
Sub != SubEnd; ++Sub)
|
|
|
|
if (std::error_code Err = collectModuleHeaderIncludes(
|
2017-06-02 09:55:39 +08:00
|
|
|
LangOpts, FileMgr, Diag, ModMap, *Sub, Includes))
|
2017-04-28 09:49:42 +08:00
|
|
|
return Err;
|
|
|
|
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2017-06-06 02:10:11 +08:00
|
|
|
static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
|
2017-06-01 04:56:55 +08:00
|
|
|
bool IsPreprocessed,
|
|
|
|
std::string &PresumedModuleMapFile,
|
|
|
|
unsigned &Offset) {
|
2017-05-06 06:18:51 +08:00
|
|
|
auto &SrcMgr = CI.getSourceManager();
|
|
|
|
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
|
2017-04-28 09:49:42 +08:00
|
|
|
|
2017-05-06 06:18:51 +08:00
|
|
|
// Map the current input to a file.
|
|
|
|
FileID ModuleMapID = SrcMgr.getMainFileID();
|
2020-12-15 11:04:41 +08:00
|
|
|
const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
|
2017-05-06 06:18:51 +08:00
|
|
|
|
|
|
|
// If the module map is preprocessed, handle the initial line marker;
|
|
|
|
// line directives are not part of the module map syntax in general.
|
|
|
|
Offset = 0;
|
|
|
|
if (IsPreprocessed) {
|
|
|
|
SourceLocation EndOfLineMarker =
|
2017-06-29 10:19:42 +08:00
|
|
|
ReadOriginalFileName(CI, PresumedModuleMapFile, /*IsModuleMap*/ true);
|
2017-05-06 06:18:51 +08:00
|
|
|
if (EndOfLineMarker.isValid())
|
|
|
|
Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second;
|
2017-04-28 09:49:42 +08:00
|
|
|
}
|
2017-05-06 06:18:51 +08:00
|
|
|
|
|
|
|
// Load the module map file.
|
2020-12-15 11:04:41 +08:00
|
|
|
if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
|
2017-06-01 04:56:55 +08:00
|
|
|
PresumedModuleMapFile))
|
2017-05-06 06:18:51 +08:00
|
|
|
return true;
|
|
|
|
|
2020-10-15 01:28:34 +08:00
|
|
|
if (SrcMgr.getBufferOrFake(ModuleMapID).getBufferSize() == Offset)
|
2017-05-06 06:18:51 +08:00
|
|
|
Offset = 0;
|
|
|
|
|
2022-02-23 21:15:47 +08:00
|
|
|
// Infer framework module if possible.
|
|
|
|
if (HS.getModuleMap().canInferFrameworkModule(ModuleMap->getDir())) {
|
|
|
|
SmallString<128> InferredFrameworkPath = ModuleMap->getDir()->getName();
|
|
|
|
llvm::sys::path::append(InferredFrameworkPath,
|
|
|
|
CI.getLangOpts().ModuleName + ".framework");
|
|
|
|
if (auto Dir = CI.getFileManager().getDirectory(InferredFrameworkPath))
|
|
|
|
(void)HS.getModuleMap().inferFrameworkModule(*Dir, IsSystem, nullptr);
|
|
|
|
}
|
|
|
|
|
2017-05-06 06:18:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Module *prepareToBuildModule(CompilerInstance &CI,
|
|
|
|
StringRef ModuleMapFilename) {
|
2017-04-28 09:49:42 +08:00
|
|
|
if (CI.getLangOpts().CurrentModule.empty()) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_missing_module_name);
|
2017-05-06 06:18:51 +08:00
|
|
|
|
2017-04-28 09:49:42 +08:00
|
|
|
// FIXME: Eventually, we could consider asking whether there was just
|
2018-07-31 03:24:48 +08:00
|
|
|
// a single module described in the module map, and use that as a
|
2017-04-28 09:49:42 +08:00
|
|
|
// default. Then it would be fairly trivial to just "compile" a module
|
|
|
|
// map with a single module (the common case).
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dig out the module definition.
|
2017-05-06 06:18:51 +08:00
|
|
|
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
|
2021-10-12 15:04:37 +08:00
|
|
|
Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule, SourceLocation(),
|
2021-05-17 15:40:29 +08:00
|
|
|
/*AllowSearch=*/true);
|
2017-04-28 09:49:42 +08:00
|
|
|
if (!M) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_missing_module)
|
2017-05-06 06:18:51 +08:00
|
|
|
<< CI.getLangOpts().CurrentModule << ModuleMapFilename;
|
|
|
|
|
2017-04-28 09:49:42 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether we can build this module at all.
|
2017-06-06 02:57:56 +08:00
|
|
|
if (Preprocessor::checkModuleIsAvailable(CI.getLangOpts(), CI.getTarget(),
|
|
|
|
CI.getDiagnostics(), M))
|
2017-04-28 09:49:42 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2017-05-06 06:18:51 +08:00
|
|
|
// Inform the preprocessor that includes from within the input buffer should
|
|
|
|
// be resolved relative to the build directory of the module map file.
|
|
|
|
CI.getPreprocessor().setMainFileDir(M->Directory);
|
|
|
|
|
|
|
|
// If the module was inferred from a different module map (via an expanded
|
|
|
|
// umbrella module definition), track that fact.
|
|
|
|
// FIXME: It would be preferable to fill this in as part of processing
|
|
|
|
// the module map, rather than adding it after the fact.
|
|
|
|
StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap;
|
|
|
|
if (!OriginalModuleMapName.empty()) {
|
2019-08-02 05:31:56 +08:00
|
|
|
auto OriginalModuleMap =
|
2017-05-06 06:18:51 +08:00
|
|
|
CI.getFileManager().getFile(OriginalModuleMapName,
|
|
|
|
/*openFile*/ true);
|
|
|
|
if (!OriginalModuleMap) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_map_not_found)
|
|
|
|
<< OriginalModuleMapName;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-08-02 05:31:56 +08:00
|
|
|
if (*OriginalModuleMap != CI.getSourceManager().getFileEntryForID(
|
2017-05-06 06:18:51 +08:00
|
|
|
CI.getSourceManager().getMainFileID())) {
|
|
|
|
M->IsInferred = true;
|
|
|
|
CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()
|
2019-08-02 05:31:56 +08:00
|
|
|
.setInferredModuleAllowedBy(M, *OriginalModuleMap);
|
2017-05-06 06:18:51 +08:00
|
|
|
}
|
2017-04-28 09:49:42 +08:00
|
|
|
}
|
|
|
|
|
2017-05-06 06:18:51 +08:00
|
|
|
// If we're being run from the command-line, the module build stack will not
|
|
|
|
// have been filled in yet, so complete it now in order to allow us to detect
|
|
|
|
// module cycles.
|
|
|
|
SourceManager &SourceMgr = CI.getSourceManager();
|
|
|
|
if (SourceMgr.getModuleBuildStack().empty())
|
|
|
|
SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
|
|
|
|
FullSourceLoc(SourceLocation(), SourceMgr));
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compute the input buffer that should be used to build the specified module.
|
|
|
|
static std::unique_ptr<llvm::MemoryBuffer>
|
|
|
|
getInputBufferForModule(CompilerInstance &CI, Module *M) {
|
2017-04-28 09:49:42 +08:00
|
|
|
FileManager &FileMgr = CI.getFileManager();
|
|
|
|
|
|
|
|
// Collect the set of #includes we need to build the module.
|
|
|
|
SmallString<256> HeaderContents;
|
|
|
|
std::error_code Err = std::error_code();
|
|
|
|
if (Module::Header UmbrellaHeader = M->getUmbrellaHeader())
|
2021-05-17 15:40:29 +08:00
|
|
|
addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
|
|
|
|
HeaderContents, CI.getLangOpts(), M->IsExternC);
|
2017-04-28 09:49:42 +08:00
|
|
|
Err = collectModuleHeaderIncludes(
|
2017-06-02 09:55:39 +08:00
|
|
|
CI.getLangOpts(), FileMgr, CI.getDiagnostics(),
|
2017-04-28 09:49:42 +08:00
|
|
|
CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M,
|
|
|
|
HeaderContents);
|
|
|
|
|
|
|
|
if (Err) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
|
|
|
|
<< M->getFullModuleName() << Err.message();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm::MemoryBuffer::getMemBufferCopy(
|
|
|
|
HeaderContents, Module::getModuleInputBufferName());
|
|
|
|
}
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
|
2017-06-06 02:10:11 +08:00
|
|
|
const FrontendInputFile &RealInput) {
|
|
|
|
FrontendInputFile Input(RealInput);
|
2009-11-14 18:42:35 +08:00
|
|
|
assert(!Instance && "Already processing a source file!");
|
2012-11-10 03:40:39 +08:00
|
|
|
assert(!Input.isEmpty() && "Unexpected empty filename!");
|
2012-01-21 00:28:04 +08:00
|
|
|
setCurrentInput(Input);
|
2009-11-14 18:42:35 +08:00
|
|
|
setCompilerInstance(&CI);
|
|
|
|
|
2012-08-10 09:06:08 +08:00
|
|
|
bool HasBegunSourceFile = false;
|
2017-06-06 02:10:11 +08:00
|
|
|
bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled &&
|
|
|
|
usesPreprocessorOnly();
|
2021-09-16 09:42:12 +08:00
|
|
|
|
|
|
|
// If we fail, reset state since the client will not end up calling the
|
|
|
|
// matching EndSourceFile(). All paths that return true should release this.
|
|
|
|
auto FailureCleanup = llvm::make_scope_exit([&]() {
|
|
|
|
if (HasBegunSourceFile)
|
|
|
|
CI.getDiagnosticClient().EndSourceFile();
|
|
|
|
CI.clearOutputFiles(/*EraseFiles=*/true);
|
|
|
|
CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
|
|
|
|
setCurrentInput(FrontendInputFile());
|
|
|
|
setCompilerInstance(nullptr);
|
|
|
|
});
|
|
|
|
|
2011-06-18 08:53:41 +08:00
|
|
|
if (!BeginInvocation(CI))
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2011-06-18 08:53:41 +08:00
|
|
|
|
2017-06-06 02:10:11 +08:00
|
|
|
// If we're replaying the build of an AST file, import it and set up
|
|
|
|
// the initial state from its build.
|
|
|
|
if (ReplayASTFile) {
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
|
|
|
|
|
|
|
|
// The AST unit populates its own diagnostics engine rather than ours.
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags(
|
|
|
|
new DiagnosticsEngine(Diags->getDiagnosticIDs(),
|
|
|
|
&Diags->getDiagnosticOptions()));
|
|
|
|
ASTDiags->setClient(Diags->getClient(), /*OwnsClient*/false);
|
|
|
|
|
2018-09-15 09:21:15 +08:00
|
|
|
// FIXME: What if the input is a memory buffer?
|
|
|
|
StringRef InputFile = Input.getFile();
|
|
|
|
|
2017-06-06 02:10:11 +08:00
|
|
|
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string(InputFile), CI.getPCHContainerReader(),
|
|
|
|
ASTUnit::LoadPreprocessorOnly, ASTDiags, CI.getFileSystemOpts(),
|
|
|
|
CI.getCodeGenOpts().DebugTypeExtRefs);
|
2017-06-06 02:10:11 +08:00
|
|
|
if (!AST)
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2017-06-06 02:10:11 +08:00
|
|
|
|
|
|
|
// Options relating to how we treat the input (but not what we do with it)
|
|
|
|
// are inherited from the AST unit.
|
2017-06-06 08:32:01 +08:00
|
|
|
CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts();
|
|
|
|
CI.getPreprocessorOpts() = AST->getPreprocessorOpts();
|
2017-06-06 02:10:11 +08:00
|
|
|
CI.getLangOpts() = AST->getLangOpts();
|
|
|
|
|
|
|
|
// 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.createSourceManager(CI.getFileManager());
|
|
|
|
CI.getSourceManager().initializeForReplay(AST->getSourceManager());
|
|
|
|
|
2017-06-29 10:19:42 +08:00
|
|
|
// Preload all the module files loaded transitively by the AST unit. Also
|
|
|
|
// load all module map files that were parsed as part of building the AST
|
|
|
|
// unit.
|
|
|
|
if (auto ASTReader = AST->getASTReader()) {
|
|
|
|
auto &MM = ASTReader->getModuleManager();
|
|
|
|
auto &PrimaryModule = MM.getPrimaryModule();
|
|
|
|
|
2018-10-12 20:21:29 +08:00
|
|
|
for (serialization::ModuleFile &MF : MM)
|
2017-06-29 10:19:42 +08:00
|
|
|
if (&MF != &PrimaryModule)
|
|
|
|
CI.getFrontendOpts().ModuleFiles.push_back(MF.FileName);
|
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
ASTReader->visitTopLevelModuleMaps(
|
|
|
|
PrimaryModule, [&](const FileEntry *FE) {
|
|
|
|
CI.getFrontendOpts().ModuleMapFiles.push_back(
|
|
|
|
std::string(FE->getName()));
|
|
|
|
});
|
2017-06-29 10:19:42 +08:00
|
|
|
}
|
|
|
|
|
2017-06-06 02:10:11 +08:00
|
|
|
// Set up the input file for replay purposes.
|
|
|
|
auto Kind = AST->getInputKind();
|
|
|
|
if (Kind.getFormat() == InputKind::ModuleMap) {
|
|
|
|
Module *ASTModule =
|
|
|
|
AST->getPreprocessor().getHeaderSearchInfo().lookupModule(
|
2021-10-12 15:04:37 +08:00
|
|
|
AST->getLangOpts().CurrentModule, SourceLocation(),
|
|
|
|
/*AllowSearch*/ false);
|
2017-06-30 07:23:46 +08:00
|
|
|
assert(ASTModule && "module file does not define its own module");
|
2017-06-06 02:10:11 +08:00
|
|
|
Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind);
|
|
|
|
} else {
|
2018-09-15 09:21:18 +08:00
|
|
|
auto &OldSM = AST->getSourceManager();
|
|
|
|
FileID ID = OldSM.getMainFileID();
|
|
|
|
if (auto *File = OldSM.getFileEntryForID(ID))
|
2017-06-06 02:10:11 +08:00
|
|
|
Input = FrontendInputFile(File->getName(), Kind);
|
|
|
|
else
|
2020-10-15 05:17:34 +08:00
|
|
|
Input = FrontendInputFile(OldSM.getBufferOrFake(ID), Kind);
|
2017-06-06 02:10:11 +08:00
|
|
|
}
|
|
|
|
setCurrentInput(Input, std::move(AST));
|
|
|
|
}
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
// AST files follow a very different path, since they share objects via the
|
|
|
|
// AST unit.
|
2017-04-27 02:57:40 +08:00
|
|
|
if (Input.getKind().getFormat() == InputKind::Precompiled) {
|
2017-06-06 02:10:11 +08:00
|
|
|
assert(!usesPreprocessorOnly() && "this case was handled above");
|
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());
|
2013-11-27 13:22:15 +08:00
|
|
|
|
2018-09-15 09:21:15 +08:00
|
|
|
// FIXME: What if the input is a memory buffer?
|
|
|
|
StringRef InputFile = Input.getFile();
|
|
|
|
|
2015-08-28 03:46:20 +08:00
|
|
|
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string(InputFile), CI.getPCHContainerReader(),
|
|
|
|
ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts(),
|
|
|
|
CI.getCodeGenOpts().DebugTypeExtRefs);
|
2014-08-11 01:03:42 +08:00
|
|
|
|
2009-12-03 09:45:44 +08:00
|
|
|
if (!AST)
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2009-11-14 18:42:35 +08:00
|
|
|
|
2013-03-19 06:55:24 +08:00
|
|
|
// Inform the diagnostic client we are processing a source file.
|
2014-05-22 12:46:25 +08:00
|
|
|
CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
|
2013-03-19 06:55:24 +08:00
|
|
|
HasBegunSourceFile = true;
|
|
|
|
|
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());
|
2017-01-06 03:48:07 +08:00
|
|
|
CI.setPreprocessor(AST->getPreprocessorPtr());
|
2017-02-09 04:51:11 +08:00
|
|
|
Preprocessor &PP = CI.getPreprocessor();
|
|
|
|
PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
|
|
|
|
PP.getLangOpts());
|
2009-11-14 18:42:35 +08:00
|
|
|
CI.setASTContext(&AST->getASTContext());
|
|
|
|
|
2014-08-11 01:03:42 +08:00
|
|
|
setCurrentInput(Input, std::move(AST));
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
// Initialize the action.
|
2017-06-09 09:36:10 +08:00
|
|
|
if (!BeginSourceFileAction(CI))
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2009-11-14 18:42:35 +08:00
|
|
|
|
2013-01-23 08:45:44 +08:00
|
|
|
// Create the AST consumer.
|
2012-11-10 03:40:39 +08:00
|
|
|
CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
|
2009-11-14 18:42:35 +08:00
|
|
|
if (!CI.hasASTConsumer())
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2009-11-14 18:42:35 +08:00
|
|
|
|
2021-09-16 09:42:12 +08:00
|
|
|
FailureCleanup.release();
|
2009-11-14 18:42:35 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-13 00:54:53 +08:00
|
|
|
// Set up the file and source managers, if needed.
|
|
|
|
if (!CI.hasFileManager()) {
|
|
|
|
if (!CI.createFileManager()) {
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2017-09-13 00:54:53 +08:00
|
|
|
}
|
2014-02-26 02:23:47 +08:00
|
|
|
}
|
2010-06-08 07:23:50 +08:00
|
|
|
if (!CI.hasSourceManager())
|
2010-11-23 16:35:12 +08:00
|
|
|
CI.createSourceManager(CI.getFileManager());
|
2010-06-08 07:26:47 +08:00
|
|
|
|
2017-04-28 09:49:42 +08:00
|
|
|
// Set up embedding for any specified files. Do this before we load any
|
|
|
|
// source files, including the primary module map for the compilation.
|
|
|
|
for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
|
2019-08-02 05:31:56 +08:00
|
|
|
if (auto FE = CI.getFileManager().getFile(F, /*openFile*/true))
|
|
|
|
CI.getSourceManager().setFileIsTransient(*FE);
|
2017-04-28 09:49:42 +08:00
|
|
|
else
|
|
|
|
CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
|
|
|
|
}
|
|
|
|
if (CI.getFrontendOpts().ModulesEmbedAllFiles)
|
|
|
|
CI.getSourceManager().setAllFilesAreTransient(true);
|
|
|
|
|
2010-06-08 07:26:47 +08:00
|
|
|
// IR files bypass the rest of initialization.
|
2019-08-05 21:59:26 +08:00
|
|
|
if (Input.getKind().getLanguage() == Language::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.
|
2014-05-22 12:46:25 +08:00
|
|
|
CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
|
2012-08-10 09:06:08 +08:00
|
|
|
HasBegunSourceFile = true;
|
2010-06-08 07:26:47 +08:00
|
|
|
|
|
|
|
// Initialize the action.
|
2017-06-09 09:36:10 +08:00
|
|
|
if (!BeginSourceFileAction(CI))
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2010-06-08 07:26:47 +08:00
|
|
|
|
2014-04-15 02:00:01 +08:00
|
|
|
// Initialize the main file entry.
|
|
|
|
if (!CI.InitializeSourceManager(CurrentInput))
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2014-04-15 02:00:01 +08:00
|
|
|
|
2021-09-16 09:42:12 +08:00
|
|
|
FailureCleanup.release();
|
2010-06-08 07:26:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:18:24 +08:00
|
|
|
// If the implicit PCH include is actually a directory, rather than
|
|
|
|
// a single file, search for a suitable PCH file in that directory.
|
|
|
|
if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
|
|
|
|
FileManager &FileMgr = CI.getFileManager();
|
|
|
|
PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
|
|
|
|
StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
|
2015-02-20 04:12:20 +08:00
|
|
|
std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
|
2022-04-21 00:54:36 +08:00
|
|
|
if (auto PCHDir = FileMgr.getOptionalDirectoryRef(PCHInclude)) {
|
2014-06-12 22:02:15 +08:00
|
|
|
std::error_code EC;
|
2012-10-23 14:18:24 +08:00
|
|
|
SmallString<128> DirNative;
|
2022-04-21 00:54:36 +08:00
|
|
|
llvm::sys::path::native(PCHDir->getName(), DirNative);
|
2012-10-23 14:18:24 +08:00
|
|
|
bool Found = false;
|
2019-03-27 06:32:06 +08:00
|
|
|
llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
|
2018-10-10 21:27:25 +08:00
|
|
|
for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
|
|
|
|
DirEnd;
|
2012-10-23 14:18:24 +08:00
|
|
|
Dir != DirEnd && !EC; Dir.increment(EC)) {
|
|
|
|
// Check whether this is an acceptable AST file.
|
2015-06-21 02:53:08 +08:00
|
|
|
if (ASTReader::isAcceptableASTFile(
|
2018-09-14 20:47:38 +08:00
|
|
|
Dir->path(), FileMgr, CI.getPCHContainerReader(),
|
2015-06-21 02:53:08 +08:00
|
|
|
CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
|
|
|
|
SpecificModuleCachePath)) {
|
2020-01-29 03:23:46 +08:00
|
|
|
PPOpts.ImplicitPCHInclude = std::string(Dir->path());
|
2013-02-06 00:36:52 +08:00
|
|
|
Found = true;
|
2012-10-23 14:18:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Found) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2012-10-23 14:18:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add support for the static analyzer to synthesize function implementations from external model files.
Currently the analyzer lazily models some functions using 'BodyFarm',
which constructs a fake function implementation that the analyzer
can simulate that approximates the semantics of the function when
it is called. BodyFarm does this by constructing the AST for
such definitions on-the-fly. One strength of BodyFarm
is that all symbols and types referenced by synthesized function
bodies are contextual adapted to the containing translation unit.
The downside is that these ASTs are hardcoded in Clang's own
source code.
A more scalable model is to allow these models to be defined as source
code in separate "model" files and have the analyzer use those
definitions lazily when a function body is needed. Among other things,
it will allow more customization of the analyzer for specific APIs
and platforms.
This patch provides the initial infrastructure for this feature.
It extends BodyFarm to use an abstract API 'CodeInjector' that can be
used to synthesize function bodies. That 'CodeInjector' is
implemented using a new 'ModelInjector' in libFrontend, which lazily
parses a model file and injects the ASTs into the current translation
unit.
Models are currently found by specifying a 'model-path' as an
analyzer option; if no path is specified the CodeInjector is not
used, thus defaulting to the current behavior in the analyzer.
Models currently contain a single function definition, and can
be found by finding the file <function name>.model. This is an
initial starting point for something more rich, but it bootstraps
this feature for future evolution.
This patch was contributed by Gábor Horváth as part of his
Google Summer of Code project.
Some notes:
- This introduces the notion of a "model file" into
FrontendAction and the Preprocessor. This nomenclature
is specific to the static analyzer, but possibly could be
generalized. Essentially these are sources pulled in
exogenously from the principal translation.
Preprocessor gets a 'InitializeForModelFile' and
'FinalizeForModelFile' which could possibly be hoisted out
of Preprocessor if Preprocessor exposed a new API to
change the PragmaHandlers and some other internal pieces. This
can be revisited.
FrontendAction gets a 'isModelParsingAction()' predicate function
used to allow a new FrontendAction to recycle the Preprocessor
and ASTContext. This name could probably be made something
more general (i.e., not tied to 'model files') at the expense
of losing the intent of why it exists. This can be revisited.
- This is a moderate sized patch; it has gone through some amount of
offline code review. Most of the changes to the non-analyzer
parts are fairly small, and would make little sense without
the analyzer changes.
- Most of the analyzer changes are plumbing, with the interesting
behavior being introduced by ModelInjector.cpp and
ModelConsumer.cpp.
- The new functionality introduced by this change is off-by-default.
It requires an analyzer config option to enable.
llvm-svn: 216550
2014-08-27 23:14:15 +08:00
|
|
|
// Set up the preprocessor if needed. When parsing model files the
|
|
|
|
// preprocessor of the original source is reused.
|
|
|
|
if (!isModelParsingAction())
|
|
|
|
CI.createPreprocessor(getTranslationUnitKind());
|
2010-06-08 07:23:50 +08:00
|
|
|
|
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());
|
2012-08-10 09:06:08 +08:00
|
|
|
HasBegunSourceFile = true;
|
2009-11-14 18:42:35 +08:00
|
|
|
|
2021-01-10 21:50:26 +08:00
|
|
|
// Handle C++20 header units.
|
|
|
|
// Here, the user has the option to specify that the header name should be
|
|
|
|
// looked up in the pre-processor search paths (and the main filename as
|
|
|
|
// passed by the driver might therefore be incomplete until that look-up).
|
|
|
|
if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
|
|
|
|
!Input.getKind().isPreprocessed()) {
|
|
|
|
StringRef FileName = Input.getFile();
|
|
|
|
InputKind Kind = Input.getKind();
|
|
|
|
if (Kind.getHeaderUnitKind() != InputKind::HeaderUnit_Abs) {
|
|
|
|
assert(CI.hasPreprocessor() &&
|
|
|
|
"trying to build a header unit without a Pre-processor?");
|
|
|
|
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
|
|
|
|
// Relative searches begin from CWD.
|
|
|
|
const DirectoryEntry *Dir = nullptr;
|
|
|
|
if (auto DirOrErr = CI.getFileManager().getDirectory("."))
|
|
|
|
Dir = *DirOrErr;
|
|
|
|
SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 1> CWD;
|
|
|
|
CWD.push_back({nullptr, Dir});
|
|
|
|
Optional<FileEntryRef> FE =
|
|
|
|
HS.LookupFile(FileName, SourceLocation(),
|
|
|
|
/*Angled*/ Input.getKind().getHeaderUnitKind() ==
|
|
|
|
InputKind::HeaderUnit_System,
|
|
|
|
nullptr, nullptr, CWD, nullptr, nullptr, nullptr,
|
|
|
|
nullptr, nullptr, nullptr);
|
|
|
|
if (!FE) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
|
|
|
|
<< FileName;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// We now have the filename...
|
|
|
|
FileName = FE->getFileEntry().getName();
|
|
|
|
// ... still a header unit, but now use the path as written.
|
|
|
|
Kind = Input.getKind().withHeaderUnit(InputKind::HeaderUnit_Abs);
|
|
|
|
Input = FrontendInputFile(FileName, Kind, Input.isSystem());
|
|
|
|
}
|
|
|
|
// Unless the user has overridden the name, the header unit module name is
|
|
|
|
// the pathname for the file.
|
|
|
|
if (CI.getLangOpts().ModuleName.empty())
|
|
|
|
CI.getLangOpts().ModuleName = std::string(FileName);
|
|
|
|
CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
|
|
|
|
}
|
|
|
|
|
2017-05-06 06:18:51 +08:00
|
|
|
if (!CI.InitializeSourceManager(Input))
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2017-05-06 06:18:51 +08:00
|
|
|
|
2022-03-03 04:18:10 +08:00
|
|
|
if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
|
|
|
|
Input.getKind().isPreprocessed() && !usesPreprocessorOnly()) {
|
|
|
|
// We have an input filename like foo.iih, but we want to find the right
|
|
|
|
// module name (and original file, to build the map entry).
|
|
|
|
// Check if the first line specifies the original source file name with a
|
|
|
|
// linemarker.
|
|
|
|
std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
|
|
|
|
ReadOriginalFileName(CI, PresumedInputFile);
|
|
|
|
// Unless the user overrides this, the module name is the name by which the
|
|
|
|
// original file was known.
|
|
|
|
if (CI.getLangOpts().ModuleName.empty())
|
|
|
|
CI.getLangOpts().ModuleName = std::string(PresumedInputFile);
|
|
|
|
CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
|
|
|
|
}
|
|
|
|
|
2017-04-28 09:49:42 +08:00
|
|
|
// For module map files, we first parse the module map and synthesize a
|
|
|
|
// "<module-includes>" buffer before more conventional processing.
|
|
|
|
if (Input.getKind().getFormat() == InputKind::ModuleMap) {
|
|
|
|
CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
|
|
|
|
|
2017-06-01 04:56:55 +08:00
|
|
|
std::string PresumedModuleMapFile;
|
2017-05-06 06:18:51 +08:00
|
|
|
unsigned OffsetToContents;
|
2017-06-06 02:10:11 +08:00
|
|
|
if (loadModuleMapForModuleBuild(CI, Input.isSystem(),
|
2017-06-01 04:56:55 +08:00
|
|
|
Input.isPreprocessed(),
|
|
|
|
PresumedModuleMapFile, OffsetToContents))
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2017-04-28 09:49:42 +08:00
|
|
|
|
2017-05-06 06:18:51 +08:00
|
|
|
auto *CurrentModule = prepareToBuildModule(CI, Input.getFile());
|
|
|
|
if (!CurrentModule)
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2017-05-06 06:18:51 +08:00
|
|
|
|
2017-06-01 04:56:55 +08:00
|
|
|
CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile;
|
|
|
|
|
2017-05-06 06:18:51 +08:00
|
|
|
if (OffsetToContents)
|
|
|
|
// If the module contents are in the same file, skip to them.
|
|
|
|
CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true);
|
|
|
|
else {
|
|
|
|
// Otherwise, convert the module description to a suitable input buffer.
|
|
|
|
auto Buffer = getInputBufferForModule(CI, CurrentModule);
|
|
|
|
if (!Buffer)
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2017-05-06 06:18:51 +08:00
|
|
|
|
|
|
|
// Reinitialize the main file entry to refer to the new input.
|
2017-09-09 09:14:04 +08:00
|
|
|
auto Kind = CurrentModule->IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
|
|
|
|
auto &SourceMgr = CI.getSourceManager();
|
|
|
|
auto BufferID = SourceMgr.createFileID(std::move(Buffer), Kind);
|
2020-04-06 23:49:51 +08:00
|
|
|
assert(BufferID.isValid() && "couldn't create module buffer ID");
|
2017-09-09 09:14:04 +08:00
|
|
|
SourceMgr.setMainFileID(BufferID);
|
2017-05-06 06:18:51 +08:00
|
|
|
}
|
2017-04-28 09:49:42 +08:00
|
|
|
}
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
// Initialize the action.
|
2017-06-09 09:36:10 +08:00
|
|
|
if (!BeginSourceFileAction(CI))
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2009-11-14 18:42:35 +08:00
|
|
|
|
2018-07-19 20:32:06 +08:00
|
|
|
// If we were asked to load any module map files, do so now.
|
|
|
|
for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
|
2020-12-15 11:04:41 +08:00
|
|
|
if (auto File = CI.getFileManager().getFile(Filename))
|
2018-07-19 20:32:06 +08:00
|
|
|
CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
|
2019-08-02 05:31:56 +08:00
|
|
|
*File, /*IsSystem*/false);
|
2018-07-19 20:32:06 +08:00
|
|
|
else
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a module declaration scope so that modules from -fmodule-map-file
|
|
|
|
// arguments may shadow modules found implicitly in search paths.
|
|
|
|
CI.getPreprocessor()
|
|
|
|
.getHeaderSearchInfo()
|
|
|
|
.getModuleMap()
|
|
|
|
.finishModuleDeclarationScope();
|
|
|
|
|
2013-01-23 08:45:44 +08:00
|
|
|
// Create the AST context and consumer unless this is a preprocessor only
|
|
|
|
// action.
|
2009-11-14 18:42:35 +08:00
|
|
|
if (!usesPreprocessorOnly()) {
|
Add support for the static analyzer to synthesize function implementations from external model files.
Currently the analyzer lazily models some functions using 'BodyFarm',
which constructs a fake function implementation that the analyzer
can simulate that approximates the semantics of the function when
it is called. BodyFarm does this by constructing the AST for
such definitions on-the-fly. One strength of BodyFarm
is that all symbols and types referenced by synthesized function
bodies are contextual adapted to the containing translation unit.
The downside is that these ASTs are hardcoded in Clang's own
source code.
A more scalable model is to allow these models to be defined as source
code in separate "model" files and have the analyzer use those
definitions lazily when a function body is needed. Among other things,
it will allow more customization of the analyzer for specific APIs
and platforms.
This patch provides the initial infrastructure for this feature.
It extends BodyFarm to use an abstract API 'CodeInjector' that can be
used to synthesize function bodies. That 'CodeInjector' is
implemented using a new 'ModelInjector' in libFrontend, which lazily
parses a model file and injects the ASTs into the current translation
unit.
Models are currently found by specifying a 'model-path' as an
analyzer option; if no path is specified the CodeInjector is not
used, thus defaulting to the current behavior in the analyzer.
Models currently contain a single function definition, and can
be found by finding the file <function name>.model. This is an
initial starting point for something more rich, but it bootstraps
this feature for future evolution.
This patch was contributed by Gábor Horváth as part of his
Google Summer of Code project.
Some notes:
- This introduces the notion of a "model file" into
FrontendAction and the Preprocessor. This nomenclature
is specific to the static analyzer, but possibly could be
generalized. Essentially these are sources pulled in
exogenously from the principal translation.
Preprocessor gets a 'InitializeForModelFile' and
'FinalizeForModelFile' which could possibly be hoisted out
of Preprocessor if Preprocessor exposed a new API to
change the PragmaHandlers and some other internal pieces. This
can be revisited.
FrontendAction gets a 'isModelParsingAction()' predicate function
used to allow a new FrontendAction to recycle the Preprocessor
and ASTContext. This name could probably be made something
more general (i.e., not tied to 'model files') at the expense
of losing the intent of why it exists. This can be revisited.
- This is a moderate sized patch; it has gone through some amount of
offline code review. Most of the changes to the non-analyzer
parts are fairly small, and would make little sense without
the analyzer changes.
- Most of the analyzer changes are plumbing, with the interesting
behavior being introduced by ModelInjector.cpp and
ModelConsumer.cpp.
- The new functionality introduced by this change is off-by-default.
It requires an analyzer config option to enable.
llvm-svn: 216550
2014-08-27 23:14:15 +08:00
|
|
|
// Parsing a model file should reuse the existing ASTContext.
|
|
|
|
if (!isModelParsingAction())
|
|
|
|
CI.createASTContext();
|
2009-11-14 18:42:35 +08:00
|
|
|
|
2017-03-08 04:20:23 +08:00
|
|
|
// For preprocessed files, check if the first line specifies the original
|
|
|
|
// source file name with a linemarker.
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
|
2017-03-08 04:20:23 +08:00
|
|
|
if (Input.isPreprocessed())
|
2017-05-06 06:18:51 +08:00
|
|
|
ReadOriginalFileName(CI, PresumedInputFile);
|
2017-03-08 04:20:23 +08:00
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer> Consumer =
|
2017-05-06 06:18:51 +08:00
|
|
|
CreateWrappedASTConsumer(CI, PresumedInputFile);
|
2010-10-30 03:49:13 +08:00
|
|
|
if (!Consumer)
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2010-07-30 08:29:29 +08:00
|
|
|
|
Add support for the static analyzer to synthesize function implementations from external model files.
Currently the analyzer lazily models some functions using 'BodyFarm',
which constructs a fake function implementation that the analyzer
can simulate that approximates the semantics of the function when
it is called. BodyFarm does this by constructing the AST for
such definitions on-the-fly. One strength of BodyFarm
is that all symbols and types referenced by synthesized function
bodies are contextual adapted to the containing translation unit.
The downside is that these ASTs are hardcoded in Clang's own
source code.
A more scalable model is to allow these models to be defined as source
code in separate "model" files and have the analyzer use those
definitions lazily when a function body is needed. Among other things,
it will allow more customization of the analyzer for specific APIs
and platforms.
This patch provides the initial infrastructure for this feature.
It extends BodyFarm to use an abstract API 'CodeInjector' that can be
used to synthesize function bodies. That 'CodeInjector' is
implemented using a new 'ModelInjector' in libFrontend, which lazily
parses a model file and injects the ASTs into the current translation
unit.
Models are currently found by specifying a 'model-path' as an
analyzer option; if no path is specified the CodeInjector is not
used, thus defaulting to the current behavior in the analyzer.
Models currently contain a single function definition, and can
be found by finding the file <function name>.model. This is an
initial starting point for something more rich, but it bootstraps
this feature for future evolution.
This patch was contributed by Gábor Horváth as part of his
Google Summer of Code project.
Some notes:
- This introduces the notion of a "model file" into
FrontendAction and the Preprocessor. This nomenclature
is specific to the static analyzer, but possibly could be
generalized. Essentially these are sources pulled in
exogenously from the principal translation.
Preprocessor gets a 'InitializeForModelFile' and
'FinalizeForModelFile' which could possibly be hoisted out
of Preprocessor if Preprocessor exposed a new API to
change the PragmaHandlers and some other internal pieces. This
can be revisited.
FrontendAction gets a 'isModelParsingAction()' predicate function
used to allow a new FrontendAction to recycle the Preprocessor
and ASTContext. This name could probably be made something
more general (i.e., not tied to 'model files') at the expense
of losing the intent of why it exists. This can be revisited.
- This is a moderate sized patch; it has gone through some amount of
offline code review. Most of the changes to the non-analyzer
parts are fairly small, and would make little sense without
the analyzer changes.
- Most of the analyzer changes are plumbing, with the interesting
behavior being introduced by ModelInjector.cpp and
ModelConsumer.cpp.
- The new functionality introduced by this change is off-by-default.
It requires an analyzer config option to enable.
llvm-svn: 216550
2014-08-27 23:14:15 +08:00
|
|
|
// FIXME: should not overwrite ASTMutationListener when parsing model files?
|
|
|
|
if (!isModelParsingAction())
|
|
|
|
CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
|
2014-10-22 10:05:46 +08:00
|
|
|
|
2011-03-10 01:21:42 +08:00
|
|
|
if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
|
|
|
|
// Convert headers to PCH and chain them.
|
2014-07-07 19:07:10 +08:00
|
|
|
IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
|
|
|
|
source = createChainedIncludesSource(CI, FinalReader);
|
2011-03-10 01:21:42 +08:00
|
|
|
if (!source)
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2020-02-21 09:45:51 +08:00
|
|
|
CI.setASTReader(static_cast<ASTReader *>(FinalReader.get()));
|
2011-03-10 01:21:42 +08:00
|
|
|
CI.getASTContext().setExternalSource(source);
|
2017-02-08 05:49:41 +08:00
|
|
|
} else if (CI.getLangOpts().Modules ||
|
|
|
|
!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
|
|
|
|
// Use PCM or 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();
|
2014-05-08 12:26:47 +08:00
|
|
|
bool DeleteDeserialListener = false;
|
|
|
|
if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
|
|
|
|
DeserialListener = new DeserializedDeclsDumper(DeserialListener,
|
|
|
|
DeleteDeserialListener);
|
|
|
|
DeleteDeserialListener = true;
|
|
|
|
}
|
|
|
|
if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
|
|
|
|
DeserialListener = new DeserializedDeclsChecker(
|
|
|
|
CI.getASTContext(),
|
|
|
|
CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
|
|
|
|
DeserialListener, DeleteDeserialListener);
|
|
|
|
DeleteDeserialListener = true;
|
|
|
|
}
|
2017-02-08 05:49:41 +08:00
|
|
|
if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
|
|
|
|
CI.createPCHExternalASTSource(
|
|
|
|
CI.getPreprocessorOpts().ImplicitPCHInclude,
|
2021-01-22 03:19:34 +08:00
|
|
|
CI.getPreprocessorOpts().DisablePCHOrModuleValidation,
|
|
|
|
CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
|
|
|
|
DeserialListener, DeleteDeserialListener);
|
2017-02-08 05:49:41 +08:00
|
|
|
if (!CI.getASTContext().getExternalSource())
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2017-02-08 05:49:41 +08:00
|
|
|
}
|
2020-02-21 09:45:51 +08:00
|
|
|
// If modules are enabled, create the AST reader before creating
|
2017-02-08 05:49:41 +08:00
|
|
|
// any builtins, so that all declarations know that they might be
|
|
|
|
// extended by an external source.
|
|
|
|
if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
|
|
|
|
!CI.getASTContext().getExternalSource()) {
|
2019-11-22 10:25:03 +08:00
|
|
|
CI.createASTReader();
|
|
|
|
CI.getASTReader()->setDeserializationListener(DeserialListener,
|
|
|
|
DeleteDeserialListener);
|
2017-02-08 05:49:41 +08:00
|
|
|
}
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
2010-07-10 05:00:24 +08:00
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
CI.setASTConsumer(std::move(Consumer));
|
2010-07-10 05:00:24 +08:00
|
|
|
if (!CI.hasASTConsumer())
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
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.
|
2017-02-08 05:49:41 +08:00
|
|
|
if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
|
|
|
|
!CI.getASTContext().getExternalSource()) {
|
2009-11-14 18:42:35 +08:00
|
|
|
Preprocessor &PP = CI.getPreprocessor();
|
2015-08-06 09:01:12 +08:00
|
|
|
PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
|
2012-03-11 15:00:24 +08:00
|
|
|
PP.getLangOpts());
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-17 07:01:30 +08:00
|
|
|
} else {
|
|
|
|
// FIXME: If this is a problem, recover from it by creating a multiplex
|
|
|
|
// source.
|
2019-11-22 10:25:03 +08:00
|
|
|
assert((!CI.getLangOpts().Modules || CI.getASTReader()) &&
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-17 07:01:30 +08:00
|
|
|
"modules enabled but created an external source that "
|
|
|
|
"doesn't support modules");
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 07:01:16 +08:00
|
|
|
// If we were asked to load any module files, do so now.
|
|
|
|
for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
|
|
|
|
if (!CI.loadModuleFile(ModuleFile))
|
2021-09-16 09:42:12 +08:00
|
|
|
return false;
|
2014-10-22 10:05:46 +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.
|
2017-03-08 04:20:23 +08:00
|
|
|
if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
|
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
|
|
|
CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
|
2017-03-08 04:20:23 +08:00
|
|
|
IntrusiveRefCntPtr<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);
|
|
|
|
}
|
If a declaration is loaded, and then a module import adds a redeclaration, then
ensure that querying the first declaration for its most recent declaration
checks for redeclarations from the imported module.
This works as follows:
* The 'most recent' pointer on a canonical declaration grows a pointer to the
external AST source and a generation number (space- and time-optimized for
the case where there is no external source).
* Each time the 'most recent' pointer is queried, if it has an external source,
we check whether it's up to date, and update it if not.
* The ancillary data stored on the canonical declaration is allocated lazily
to avoid filling it in for declarations that end up being non-canonical.
We'll still perform a redundant (ASTContext) allocation if someone asks for
the most recent declaration from a decl before setPreviousDecl is called,
but such cases are probably all bugs, and are now easy to find.
Some finessing is still in order here -- in particular, we use a very general
mechanism for handling the DefinitionData pointer on CXXRecordData, and a more
targeted approach would be more compact.
Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was
addressing only a corner of the full problem space here. That's not covered
by this patch.
Early performance benchmarks show that this makes no measurable difference to
Clang performance without modules enabled (and fixes a major correctness issue
with modules enabled). I'll revert if a full performance comparison shows any
problems.
llvm-svn: 209046
2014-05-17 07:01:30 +08:00
|
|
|
|
2021-09-16 09:42:12 +08:00
|
|
|
FailureCleanup.release();
|
2009-11-14 18:42:35 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
llvm::Error FrontendAction::Execute() {
|
2009-11-14 18:42:35 +08:00
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
|
2009-11-29 17:57:35 +08:00
|
|
|
if (CI.hasFrontendTimer()) {
|
|
|
|
llvm::TimeRegion Timer(CI.getFrontendTimer());
|
|
|
|
ExecuteAction();
|
|
|
|
}
|
|
|
|
else ExecuteAction();
|
2012-06-08 13:48:06 +08:00
|
|
|
|
2013-01-24 06:38:11 +08:00
|
|
|
// If we are supposed to rebuild the global module index, do so now unless
|
2013-01-25 08:45:27 +08:00
|
|
|
// there were any module-build failures.
|
|
|
|
if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
|
|
|
|
CI.hasPreprocessor()) {
|
2015-08-15 08:34:15 +08:00
|
|
|
StringRef Cache =
|
|
|
|
CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
|
2019-06-27 03:50:12 +08:00
|
|
|
if (!Cache.empty()) {
|
|
|
|
if (llvm::Error Err = GlobalModuleIndex::writeIndex(
|
|
|
|
CI.getFileManager(), CI.getPCHContainerReader(), Cache)) {
|
|
|
|
// FIXME this drops the error on the floor, but
|
|
|
|
// Index/pch-from-libclang.c seems to rely on dropping at least some of
|
|
|
|
// the error conditions!
|
|
|
|
consumeError(std::move(Err));
|
|
|
|
}
|
|
|
|
}
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
return llvm::Error::success();
|
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();
|
|
|
|
|
2014-08-08 04:51:16 +08:00
|
|
|
// Inform the preprocessor we are done.
|
|
|
|
if (CI.hasPreprocessor())
|
|
|
|
CI.getPreprocessor().EndSourceFile();
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
// Finalize the action.
|
|
|
|
EndSourceFileAction();
|
|
|
|
|
2014-04-24 10:42:04 +08:00
|
|
|
// Sema references the ast consumer, so reset sema first.
|
2009-11-14 18:42:35 +08:00
|
|
|
//
|
|
|
|
// FIXME: There is more per-file stuff we could just drop here?
|
2014-04-24 10:42:04 +08:00
|
|
|
bool DisableFree = CI.getFrontendOpts().DisableFree;
|
|
|
|
if (DisableFree) {
|
2015-05-04 22:59:20 +08:00
|
|
|
CI.resetAndLeakSema();
|
|
|
|
CI.resetAndLeakASTContext();
|
2018-11-18 02:04:13 +08:00
|
|
|
llvm::BuryPointer(CI.takeASTConsumer().get());
|
2009-11-14 18:42:35 +08:00
|
|
|
} else {
|
2015-05-04 22:59:20 +08:00
|
|
|
CI.setSema(nullptr);
|
|
|
|
CI.setASTContext(nullptr);
|
2014-05-22 12:46:25 +08:00
|
|
|
CI.setASTConsumer(nullptr);
|
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";
|
|
|
|
}
|
|
|
|
|
2013-06-11 08:36:55 +08:00
|
|
|
// Cleanup the output streams, and erase the output files if instructed by the
|
|
|
|
// FrontendAction.
|
|
|
|
CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
|
2009-11-14 18:42:35 +08:00
|
|
|
|
2014-04-24 11:31:27 +08:00
|
|
|
if (isCurrentFileAST()) {
|
2015-05-04 22:59:20 +08:00
|
|
|
if (DisableFree) {
|
|
|
|
CI.resetAndLeakPreprocessor();
|
|
|
|
CI.resetAndLeakSourceManager();
|
|
|
|
CI.resetAndLeakFileManager();
|
2018-11-18 02:04:13 +08:00
|
|
|
llvm::BuryPointer(std::move(CurrentASTUnit));
|
2015-05-04 22:59:20 +08:00
|
|
|
} else {
|
|
|
|
CI.setPreprocessor(nullptr);
|
|
|
|
CI.setSourceManager(nullptr);
|
|
|
|
CI.setFileManager(nullptr);
|
|
|
|
}
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
2014-05-22 12:46:25 +08:00
|
|
|
setCompilerInstance(nullptr);
|
2012-01-21 00:28:04 +08:00
|
|
|
setCurrentInput(FrontendInputFile());
|
2017-04-28 09:49:42 +08:00
|
|
|
CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
|
2009-11-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
2013-06-11 08:36:55 +08:00
|
|
|
bool FrontendAction::shouldEraseOutputFiles() {
|
|
|
|
return getCompilerInstance().getDiagnostics().hasErrorOccurred();
|
|
|
|
}
|
|
|
|
|
2009-11-14 18:42:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility Actions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ASTFrontendAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
2013-07-28 21:23:37 +08:00
|
|
|
if (!CI.hasPreprocessor())
|
|
|
|
return;
|
2009-11-14 18:42:35 +08:00
|
|
|
|
|
|
|
// 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?
|
2014-05-22 12:46:25 +08:00
|
|
|
CodeCompleteConsumer *CompletionConsumer = nullptr;
|
2009-11-14 18:42:35 +08:00
|
|
|
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() { }
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
2009-11-14 18:42:35 +08:00
|
|
|
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
|
|
|
|
2019-01-17 08:20:43 +08:00
|
|
|
bool WrapperFrontendAction::PrepareToExecuteAction(CompilerInstance &CI) {
|
|
|
|
return WrappedAction->PrepareToExecuteAction(CI);
|
|
|
|
}
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
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);
|
|
|
|
}
|
2017-06-09 09:36:10 +08:00
|
|
|
bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) {
|
2012-01-21 00:28:04 +08:00
|
|
|
WrappedAction->setCurrentInput(getCurrentInput());
|
2011-06-18 08:53:41 +08:00
|
|
|
WrappedAction->setCompilerInstance(&CI);
|
2017-06-09 09:36:10 +08:00
|
|
|
auto Ret = WrappedAction->BeginSourceFileAction(CI);
|
2016-02-16 13:39:33 +08:00
|
|
|
// BeginSourceFileAction may change CurrentInput, e.g. during module builds.
|
|
|
|
setCurrentInput(WrappedAction->getCurrentInput());
|
|
|
|
return Ret;
|
2011-06-17 00:17:05 +08:00
|
|
|
}
|
|
|
|
void WrapperFrontendAction::ExecuteAction() {
|
|
|
|
WrappedAction->ExecuteAction();
|
|
|
|
}
|
2021-05-13 13:41:44 +08:00
|
|
|
void WrapperFrontendAction::EndSourceFile() { WrappedAction->EndSourceFile(); }
|
2011-06-17 00:17:05 +08:00
|
|
|
void WrapperFrontendAction::EndSourceFileAction() {
|
|
|
|
WrappedAction->EndSourceFileAction();
|
|
|
|
}
|
2020-04-01 06:23:01 +08:00
|
|
|
bool WrapperFrontendAction::shouldEraseOutputFiles() {
|
|
|
|
return WrappedAction->shouldEraseOutputFiles();
|
|
|
|
}
|
2011-06-17 00:17:05 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-02-08 03:28:36 +08:00
|
|
|
WrapperFrontendAction::WrapperFrontendAction(
|
|
|
|
std::unique_ptr<FrontendAction> WrappedAction)
|
|
|
|
: WrappedAction(std::move(WrappedAction)) {}
|
2011-06-17 00:17:05 +08:00
|
|
|
|