Tareq A. Siraj e0a03c3c64 cpp11-migrate: FileOverrides/Transform refactoring.
This commit include the following changes:
 - SourceOverrides is now a class
   - it simplifies the usage for the Transform class, since now the
     replacements can be applied directly to the file overrides with
     SourceOverrides::applyReplacements().
   - it contains a method applyRewrites() which was previously named
     collectResults() in Transform.cpp. The method has been "optimized"
     a bit to re-use the allocated buffer (std::string::clear() is called).
   - since the class has some logic it's now unit tested
 - Now FileOverrides is a class (not a std::map typedef) and store pointers
   to the SourceOverrides. The reason is that the SourceOverrides can't be
   copied anymore (which was already something to avoid since it's can be a
   quite large object).

Author: Guillaume Papin <guillaume.papin@epitech.eu>

Differential Revision: http://llvm-reviews.chandlerc.com/D1122

llvm-svn: 186161
2013-07-12 14:36:20 +00:00

76 lines
2.3 KiB
C++

//===-- Core/SyntaxCheck.cpp ----------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file exposes functionaliy for doing a syntax-only check on
/// files with overridden contents.
///
//===----------------------------------------------------------------------===//
#include "Core/SyntaxCheck.h"
#include "Core/FileOverrides.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/Tooling.h"
using namespace clang;
using namespace tooling;
class SyntaxCheck : public SyntaxOnlyAction {
public:
SyntaxCheck(const FileOverrides &Overrides) : Overrides(Overrides) {}
virtual bool BeginSourceFileAction(CompilerInstance &CI, StringRef Filename) {
if (!SyntaxOnlyAction::BeginSourceFileAction(CI, Filename))
return false;
FileOverrides::const_iterator I = Overrides.find(Filename);
if (I != Overrides.end())
I->second->applyOverrides(CI.getSourceManager());
return true;
}
private:
const FileOverrides &Overrides;
};
class SyntaxCheckFactory : public FrontendActionFactory {
public:
SyntaxCheckFactory(const FileOverrides &Overrides)
: Overrides(Overrides) {}
virtual FrontendAction *create() { return new SyntaxCheck(Overrides); }
private:
const FileOverrides &Overrides;
};
class SyntaxArgumentsAdjuster : public ArgumentsAdjuster {
CommandLineArguments Adjust(const CommandLineArguments &Args) {
CommandLineArguments AdjustedArgs = Args;
AdjustedArgs.push_back("-fsyntax-only");
AdjustedArgs.push_back("-std=c++11");
return AdjustedArgs;
}
};
bool doSyntaxCheck(const CompilationDatabase &Database,
const std::vector<std::string> &SourcePaths,
const FileOverrides &Overrides) {
ClangTool SyntaxTool(Database, SourcePaths);
// Ensure C++11 support is enabled.
// FIXME: This isn't necessary anymore since the Migrator requires C++11
// to be enabled in the CompilationDatabase. Remove later.
SyntaxTool.setArgumentsAdjuster(new SyntaxArgumentsAdjuster);
return SyntaxTool.run(new SyntaxCheckFactory(Overrides)) == 0;
}