2010-06-16 01:48:49 +08:00
|
|
|
//===--- FrontendActions.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
|
2010-06-16 01:48:49 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-09-01 13:09:24 +08:00
|
|
|
#include "clang/Rewrite/Frontend/FrontendActions.h"
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2017-06-20 07:09:36 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2017-07-18 16:55:03 +08:00
|
|
|
#include "clang/Config/config.h"
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Frontend/Utils.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"
|
2012-09-01 13:09:24 +08:00
|
|
|
#include "clang/Rewrite/Frontend/ASTConsumers.h"
|
|
|
|
#include "clang/Rewrite/Frontend/FixItRewriter.h"
|
|
|
|
#include "clang/Rewrite/Frontend/Rewriters.h"
|
2017-06-10 05:24:02 +08:00
|
|
|
#include "clang/Serialization/ASTReader.h"
|
|
|
|
#include "clang/Serialization/Module.h"
|
|
|
|
#include "clang/Serialization/ModuleManager.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2012-01-26 10:40:48 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2016-05-27 22:27:13 +08:00
|
|
|
#include <utility>
|
2012-01-26 11:47:18 +08:00
|
|
|
|
2010-06-16 01:48:49 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AST Consumer Actions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2016-07-15 08:55:40 +08:00
|
|
|
if (std::unique_ptr<raw_ostream> OS =
|
|
|
|
CI.createDefaultOutputFile(false, InFile))
|
|
|
|
return CreateHTMLPrinter(std::move(OS), CI.getPreprocessor());
|
2014-05-07 14:21:57 +08:00
|
|
|
return nullptr;
|
2010-06-16 01:48:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FixItAction::FixItAction() {}
|
2015-10-20 21:23:58 +08:00
|
|
|
FixItAction::~FixItAction() {}
|
2010-06-16 01:48:49 +08:00
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
|
|
|
return llvm::make_unique<ASTConsumer>();
|
2010-06-16 01:48:49 +08:00
|
|
|
}
|
|
|
|
|
2010-10-23 00:48:22 +08:00
|
|
|
namespace {
|
2010-08-16 00:47:39 +08:00
|
|
|
class FixItRewriteInPlace : public FixItOptions {
|
|
|
|
public:
|
2015-06-18 01:47:30 +08:00
|
|
|
FixItRewriteInPlace() { InPlace = true; }
|
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
std::string RewriteFilename(const std::string &Filename, int &fd) override {
|
2015-06-18 01:47:30 +08:00
|
|
|
llvm_unreachable("don't call RewriteFilename for inplace rewrites");
|
2012-01-26 12:19:04 +08:00
|
|
|
}
|
2010-08-16 00:47:39 +08:00
|
|
|
};
|
|
|
|
|
2010-08-14 01:31:00 +08:00
|
|
|
class FixItActionSuffixInserter : public FixItOptions {
|
2010-06-16 01:48:49 +08:00
|
|
|
std::string NewSuffix;
|
|
|
|
|
|
|
|
public:
|
2010-08-14 01:31:00 +08:00
|
|
|
FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
|
2016-05-27 22:27:13 +08:00
|
|
|
: NewSuffix(std::move(NewSuffix)) {
|
|
|
|
this->FixWhatYouCan = FixWhatYouCan;
|
2010-08-14 01:31:00 +08:00
|
|
|
}
|
2010-06-16 01:48:49 +08:00
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
std::string RewriteFilename(const std::string &Filename, int &fd) override {
|
2012-01-26 12:19:04 +08:00
|
|
|
fd = -1;
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> Path(Filename);
|
2010-12-18 12:13:32 +08:00
|
|
|
llvm::sys::path::replace_extension(Path,
|
|
|
|
NewSuffix + llvm::sys::path::extension(Path));
|
|
|
|
return Path.str();
|
2010-06-16 01:48:49 +08:00
|
|
|
}
|
|
|
|
};
|
2012-01-26 10:40:48 +08:00
|
|
|
|
|
|
|
class FixItRewriteToTemp : public FixItOptions {
|
|
|
|
public:
|
2014-03-15 12:29:04 +08:00
|
|
|
std::string RewriteFilename(const std::string &Filename, int &fd) override {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> Path;
|
2013-07-06 04:00:06 +08:00
|
|
|
llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
|
2015-09-10 00:48:47 +08:00
|
|
|
llvm::sys::path::extension(Filename).drop_front(), fd,
|
2013-07-06 04:00:06 +08:00
|
|
|
Path);
|
|
|
|
return Path.str();
|
2012-01-26 10:40:48 +08:00
|
|
|
}
|
|
|
|
};
|
2010-10-23 00:48:22 +08:00
|
|
|
} // end anonymous namespace
|
2010-06-16 01:48:49 +08:00
|
|
|
|
2017-06-09 09:36:10 +08:00
|
|
|
bool FixItAction::BeginSourceFileAction(CompilerInstance &CI) {
|
2010-06-16 01:48:49 +08:00
|
|
|
const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
|
|
|
|
if (!FEOpts.FixItSuffix.empty()) {
|
2010-08-14 01:31:00 +08:00
|
|
|
FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
|
|
|
|
FEOpts.FixWhatYouCan));
|
2010-06-16 01:48:49 +08:00
|
|
|
} else {
|
2010-08-16 00:47:39 +08:00
|
|
|
FixItOpts.reset(new FixItRewriteInPlace);
|
|
|
|
FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
|
2010-06-16 01:48:49 +08:00
|
|
|
}
|
|
|
|
Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
|
2010-08-14 01:31:00 +08:00
|
|
|
CI.getLangOpts(), FixItOpts.get()));
|
2010-06-16 01:48:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FixItAction::EndSourceFileAction() {
|
|
|
|
// Otherwise rewrite all files.
|
|
|
|
Rewriter->WriteFixedFiles();
|
|
|
|
}
|
|
|
|
|
2012-01-26 10:40:48 +08:00
|
|
|
bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, std::string> > RewrittenFiles;
|
|
|
|
bool err = false;
|
|
|
|
{
|
|
|
|
const FrontendOptions &FEOpts = CI.getFrontendOpts();
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
|
2012-01-27 09:00:47 +08:00
|
|
|
if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<FixItOptions> FixItOpts;
|
2012-01-27 09:00:47 +08:00
|
|
|
if (FEOpts.FixToTemporaries)
|
|
|
|
FixItOpts.reset(new FixItRewriteToTemp());
|
|
|
|
else
|
|
|
|
FixItOpts.reset(new FixItRewriteInPlace());
|
|
|
|
FixItOpts->Silent = true;
|
|
|
|
FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
|
|
|
|
FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
|
|
|
|
FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
|
|
|
|
CI.getLangOpts(), FixItOpts.get());
|
|
|
|
FixAction->Execute();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-01-27 09:00:47 +08:00
|
|
|
err = Rewriter.WriteFixedFiles(&RewrittenFiles);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-01-27 09:00:47 +08:00
|
|
|
FixAction->EndSourceFile();
|
2014-05-07 14:21:57 +08:00
|
|
|
CI.setSourceManager(nullptr);
|
|
|
|
CI.setFileManager(nullptr);
|
2012-01-27 09:00:47 +08:00
|
|
|
} else {
|
|
|
|
err = true;
|
|
|
|
}
|
2012-01-26 10:40:48 +08:00
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
return false;
|
|
|
|
CI.getDiagnosticClient().clear();
|
2012-01-27 14:15:37 +08:00
|
|
|
CI.getDiagnostics().Reset();
|
2012-01-26 10:40:48 +08:00
|
|
|
|
|
|
|
PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
|
|
|
|
PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
|
|
|
|
RewrittenFiles.begin(), RewrittenFiles.end());
|
|
|
|
PPOpts.RemappedFilesKeepOriginalName = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:21:17 +08:00
|
|
|
#if CLANG_ENABLE_OBJC_REWRITER
|
2010-06-16 01:48:49 +08:00
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2016-07-15 08:55:40 +08:00
|
|
|
if (std::unique_ptr<raw_ostream> OS =
|
|
|
|
CI.createDefaultOutputFile(false, InFile, "cpp")) {
|
2012-06-20 14:18:46 +08:00
|
|
|
if (CI.getLangOpts().ObjCRuntime.isNonFragile())
|
2016-02-02 19:06:51 +08:00
|
|
|
return CreateModernObjCRewriter(
|
2016-07-15 08:55:40 +08:00
|
|
|
InFile, std::move(OS), CI.getDiagnostics(), CI.getLangOpts(),
|
2016-02-02 19:06:51 +08:00
|
|
|
CI.getDiagnosticOpts().NoRewriteMacros,
|
|
|
|
(CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo));
|
2016-07-15 08:55:40 +08:00
|
|
|
return CreateObjCRewriter(InFile, std::move(OS), CI.getDiagnostics(),
|
|
|
|
CI.getLangOpts(),
|
2010-06-16 01:48:49 +08:00
|
|
|
CI.getDiagnosticOpts().NoRewriteMacros);
|
2012-02-08 01:11:38 +08:00
|
|
|
}
|
2014-05-07 14:21:57 +08:00
|
|
|
return nullptr;
|
2010-06-16 01:48:49 +08:00
|
|
|
}
|
|
|
|
|
2014-07-17 00:48:33 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Actions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-06-16 01:48:49 +08:00
|
|
|
void RewriteMacrosAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_ostream> OS =
|
2018-09-15 09:21:18 +08:00
|
|
|
CI.createDefaultOutputFile(true, getCurrentFileOrBufferName());
|
2010-06-16 01:48:49 +08:00
|
|
|
if (!OS) return;
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
RewriteMacrosInInput(CI.getPreprocessor(), OS.get());
|
2010-06-16 01:48:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteTestAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_ostream> OS =
|
2018-09-15 09:21:18 +08:00
|
|
|
CI.createDefaultOutputFile(false, getCurrentFileOrBufferName());
|
2010-06-16 01:48:49 +08:00
|
|
|
if (!OS) return;
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
DoRewriteTest(CI.getPreprocessor(), OS.get());
|
2010-06-16 01:48:49 +08:00
|
|
|
}
|
2012-06-07 02:52:13 +08:00
|
|
|
|
2017-06-10 05:24:02 +08:00
|
|
|
class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {
|
|
|
|
CompilerInstance &CI;
|
|
|
|
std::weak_ptr<raw_ostream> Out;
|
|
|
|
|
|
|
|
llvm::DenseSet<const FileEntry*> Rewritten;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RewriteImportsListener(CompilerInstance &CI, std::shared_ptr<raw_ostream> Out)
|
|
|
|
: CI(CI), Out(Out) {}
|
|
|
|
|
|
|
|
void visitModuleFile(StringRef Filename,
|
|
|
|
serialization::ModuleKind Kind) override {
|
|
|
|
auto *File = CI.getFileManager().getFile(Filename);
|
|
|
|
assert(File && "missing file for loaded module?");
|
|
|
|
|
|
|
|
// Only rewrite each module file once.
|
|
|
|
if (!Rewritten.insert(File).second)
|
|
|
|
return;
|
|
|
|
|
|
|
|
serialization::ModuleFile *MF =
|
|
|
|
CI.getModuleManager()->getModuleManager().lookup(File);
|
|
|
|
assert(File && "missing module file for loaded module?");
|
|
|
|
|
|
|
|
// Not interested in PCH / preambles.
|
|
|
|
if (!MF->isModule())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto OS = Out.lock();
|
|
|
|
assert(OS && "loaded module file after finishing rewrite action?");
|
|
|
|
|
2017-06-20 07:09:36 +08:00
|
|
|
(*OS) << "#pragma clang module build ";
|
|
|
|
if (isValidIdentifier(MF->ModuleName))
|
|
|
|
(*OS) << MF->ModuleName;
|
|
|
|
else {
|
|
|
|
(*OS) << '"';
|
|
|
|
OS->write_escaped(MF->ModuleName);
|
|
|
|
(*OS) << '"';
|
|
|
|
}
|
|
|
|
(*OS) << '\n';
|
2017-06-10 05:24:02 +08:00
|
|
|
|
|
|
|
// Rewrite the contents of the module in a separate compiler instance.
|
|
|
|
CompilerInstance Instance(CI.getPCHContainerOperations(),
|
2019-03-10 01:33:56 +08:00
|
|
|
&CI.getModuleCache());
|
2017-06-10 05:24:02 +08:00
|
|
|
Instance.setInvocation(
|
|
|
|
std::make_shared<CompilerInvocation>(CI.getInvocation()));
|
|
|
|
Instance.createDiagnostics(
|
|
|
|
new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
|
|
|
|
/*ShouldOwnClient=*/true);
|
2017-06-20 09:31:53 +08:00
|
|
|
Instance.getFrontendOpts().DisableFree = false;
|
2017-06-10 05:24:02 +08:00
|
|
|
Instance.getFrontendOpts().Inputs.clear();
|
|
|
|
Instance.getFrontendOpts().Inputs.emplace_back(
|
|
|
|
Filename, InputKind(InputKind::Unknown, InputKind::Precompiled));
|
2017-06-27 03:39:25 +08:00
|
|
|
Instance.getFrontendOpts().ModuleFiles.clear();
|
|
|
|
Instance.getFrontendOpts().ModuleMapFiles.clear();
|
2017-06-10 05:24:02 +08:00
|
|
|
// Don't recursively rewrite imports. We handle them all at the top level.
|
|
|
|
Instance.getPreprocessorOutputOpts().RewriteImports = false;
|
|
|
|
|
|
|
|
llvm::CrashRecoveryContext().RunSafelyOnThread([&]() {
|
|
|
|
RewriteIncludesAction Action;
|
|
|
|
Action.OutputStream = OS;
|
|
|
|
Instance.ExecuteAction(Action);
|
|
|
|
});
|
|
|
|
|
|
|
|
(*OS) << "#pragma clang module endbuild /*" << MF->ModuleName << "*/\n";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool RewriteIncludesAction::BeginSourceFileAction(CompilerInstance &CI) {
|
|
|
|
if (!OutputStream) {
|
2018-09-15 09:21:18 +08:00
|
|
|
OutputStream =
|
|
|
|
CI.createDefaultOutputFile(true, getCurrentFileOrBufferName());
|
2017-06-10 05:24:02 +08:00
|
|
|
if (!OutputStream)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &OS = *OutputStream;
|
2012-06-07 02:52:13 +08:00
|
|
|
|
2017-05-06 06:18:51 +08:00
|
|
|
// If we're preprocessing a module map, start by dumping the contents of the
|
|
|
|
// module itself before switching to the input buffer.
|
|
|
|
auto &Input = getCurrentInput();
|
|
|
|
if (Input.getKind().getFormat() == InputKind::ModuleMap) {
|
2017-06-02 04:10:35 +08:00
|
|
|
if (Input.isFile()) {
|
2017-06-10 05:24:02 +08:00
|
|
|
OS << "# 1 \"";
|
|
|
|
OS.write_escaped(Input.getFile());
|
|
|
|
OS << "\"\n";
|
2017-06-02 04:10:35 +08:00
|
|
|
}
|
2017-06-10 05:24:02 +08:00
|
|
|
getCurrentModule()->print(OS);
|
|
|
|
OS << "#pragma clang module contents\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're rewriting imports, set up a listener to track when we import
|
|
|
|
// module files.
|
|
|
|
if (CI.getPreprocessorOutputOpts().RewriteImports) {
|
|
|
|
CI.createModuleManager();
|
|
|
|
CI.getModuleManager()->addListener(
|
|
|
|
llvm::make_unique<RewriteImportsListener>(CI, OutputStream));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteIncludesAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
|
|
|
|
// If we're rewriting imports, emit the module build output first rather
|
|
|
|
// than switching back and forth (potentially in the middle of a line).
|
|
|
|
if (CI.getPreprocessorOutputOpts().RewriteImports) {
|
|
|
|
std::string Buffer;
|
|
|
|
llvm::raw_string_ostream OS(Buffer);
|
|
|
|
|
|
|
|
RewriteIncludesInInput(CI.getPreprocessor(), &OS,
|
|
|
|
CI.getPreprocessorOutputOpts());
|
|
|
|
|
|
|
|
(*OutputStream) << OS.str();
|
|
|
|
} else {
|
|
|
|
RewriteIncludesInInput(CI.getPreprocessor(), OutputStream.get(),
|
|
|
|
CI.getPreprocessorOutputOpts());
|
2017-05-06 06:18:51 +08:00
|
|
|
}
|
|
|
|
|
2017-06-10 05:24:02 +08:00
|
|
|
OutputStream.reset();
|
2012-06-07 02:52:13 +08:00
|
|
|
}
|