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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Rewrite/FrontendActions.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Parse/Parser.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Frontend/Utils.h"
|
|
|
|
#include "clang/Rewrite/ASTConsumers.h"
|
|
|
|
#include "clang/Rewrite/FixItRewriter.h"
|
|
|
|
#include "clang/Rewrite/Rewriters.h"
|
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2010-06-16 01:48:49 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AST Consumer Actions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
llvm::StringRef InFile) {
|
|
|
|
if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
|
|
|
|
return CreateHTMLPrinter(OS, CI.getPreprocessor());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FixItAction::FixItAction() {}
|
|
|
|
FixItAction::~FixItAction() {}
|
|
|
|
|
|
|
|
ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
llvm::StringRef InFile) {
|
|
|
|
return new ASTConsumer();
|
|
|
|
}
|
|
|
|
|
2010-10-23 00:48:22 +08:00
|
|
|
namespace {
|
2010-08-16 00:47:39 +08:00
|
|
|
class FixItRewriteInPlace : public FixItOptions {
|
|
|
|
public:
|
|
|
|
std::string RewriteFilename(const std::string &Filename) { return Filename; }
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
std::string RewriteFilename(const std::string &Filename) {
|
2010-12-18 12:13:32 +08:00
|
|
|
llvm::SmallString<128> Path(Filename);
|
|
|
|
llvm::sys::path::replace_extension(Path,
|
|
|
|
NewSuffix + llvm::sys::path::extension(Path));
|
|
|
|
return Path.str();
|
2010-06-16 01:48:49 +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,
|
|
|
|
llvm::StringRef Filename) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Actions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
llvm::StringRef InFile) {
|
|
|
|
if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
|
|
|
|
return CreateObjCRewriter(InFile, OS,
|
|
|
|
CI.getDiagnostics(), CI.getLangOpts(),
|
|
|
|
CI.getDiagnosticOpts().NoRewriteMacros);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteMacrosAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
|
|
|
|
if (!OS) return;
|
|
|
|
|
|
|
|
RewriteMacrosInInput(CI.getPreprocessor(), OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteTestAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
|
|
|
|
if (!OS) return;
|
|
|
|
|
|
|
|
DoRewriteTest(CI.getPreprocessor(), OS);
|
|
|
|
}
|