2010-06-16 01:48:49 +08:00
|
|
|
//===--- FrontendActions.cpp ----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#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"
|
|
|
|
#include "clang/Parse/Parser.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"
|
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>
|
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) {
|
2011-07-23 18:55:15 +08:00
|
|
|
if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
|
2010-06-16 01:48:49 +08:00
|
|
|
return CreateHTMLPrinter(OS, CI.getPreprocessor());
|
2014-05-07 14:21:57 +08:00
|
|
|
return nullptr;
|
2010-06-16 01:48:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FixItAction::FixItAction() {}
|
|
|
|
FixItAction::~FixItAction() {}
|
|
|
|
|
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:
|
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;
|
|
|
|
return Filename;
|
|
|
|
}
|
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)
|
|
|
|
: NewSuffix(NewSuffix) {
|
|
|
|
this->FixWhatYouCan = FixWhatYouCan;
|
|
|
|
}
|
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),
|
|
|
|
llvm::sys::path::extension(Filename), fd,
|
|
|
|
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
|
|
|
|
|
|
|
bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Filename) {
|
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();
|
2012-01-26 10:40:48 +08:00
|
|
|
|
2012-01-27 09:00:47 +08:00
|
|
|
err = Rewriter.WriteFixedFiles(&RewrittenFiles);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-17 00:48:33 +08:00
|
|
|
#ifdef 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) {
|
2012-02-08 01:11:38 +08:00
|
|
|
if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) {
|
2012-06-20 14:18:46 +08:00
|
|
|
if (CI.getLangOpts().ObjCRuntime.isNonFragile())
|
2012-02-08 01:11:38 +08:00
|
|
|
return CreateModernObjCRewriter(InFile, OS,
|
|
|
|
CI.getDiagnostics(), CI.getLangOpts(),
|
2013-02-08 08:27:34 +08:00
|
|
|
CI.getDiagnosticOpts().NoRewriteMacros,
|
|
|
|
(CI.getCodeGenOpts().getDebugInfo() !=
|
|
|
|
CodeGenOptions::NoDebugInfo));
|
2010-06-16 01:48:49 +08:00
|
|
|
return CreateObjCRewriter(InFile, OS,
|
|
|
|
CI.getDiagnostics(), CI.getLangOpts(),
|
|
|
|
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();
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
|
2010-06-16 01:48:49 +08:00
|
|
|
if (!OS) return;
|
|
|
|
|
|
|
|
RewriteMacrosInInput(CI.getPreprocessor(), OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteTestAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
|
2010-06-16 01:48:49 +08:00
|
|
|
if (!OS) return;
|
|
|
|
|
|
|
|
DoRewriteTest(CI.getPreprocessor(), OS);
|
|
|
|
}
|
2012-06-07 02:52:13 +08:00
|
|
|
|
|
|
|
void RewriteIncludesAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
|
|
|
|
if (!OS) return;
|
|
|
|
|
|
|
|
RewriteIncludesInInput(CI.getPreprocessor(), OS,
|
|
|
|
CI.getPreprocessorOutputOpts());
|
|
|
|
}
|