Updated to Clang 3.5a.
Change-Id: I8127eb568f674c2e72635b639a3295381fe8af82
diff --git a/lib/Tooling/CMakeLists.txt b/lib/Tooling/CMakeLists.txt
index d29e564..a1bf964 100644
--- a/lib/Tooling/CMakeLists.txt
+++ b/lib/Tooling/CMakeLists.txt
@@ -9,22 +9,13 @@
Refactoring.cpp
RefactoringCallbacks.cpp
Tooling.cpp
- )
-add_dependencies(clangTooling
- ClangAttrClasses
- ClangAttrList
- ClangDeclNodes
- ClangDiagnosticCommon
- ClangDiagnosticFrontend
- ClangStmtNodes
- )
-
-target_link_libraries(clangTooling
- clangBasic
- clangFrontend
+ LINK_LIBS
clangAST
clangASTMatchers
+ clangBasic
+ clangDriver
+ clangFrontend
+ clangLex
clangRewriteCore
- clangRewriteFrontend
)
diff --git a/lib/Tooling/CommonOptionsParser.cpp b/lib/Tooling/CommonOptionsParser.cpp
index cce4816..e0b844c 100644
--- a/lib/Tooling/CommonOptionsParser.cpp
+++ b/lib/Tooling/CommonOptionsParser.cpp
@@ -54,12 +54,26 @@
"\n";
CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv,
+ cl::OptionCategory &Category,
const char *Overview) {
- static cl::opt<std::string> BuildPath(
- "p", cl::desc("Build path"), cl::Optional);
+ static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
+
+ static cl::opt<std::string> BuildPath("p", cl::desc("Build path"),
+ cl::Optional, cl::cat(Category));
static cl::list<std::string> SourcePaths(
- cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore);
+ cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore,
+ cl::cat(Category));
+
+ // Hide unrelated options.
+ StringMap<cl::Option*> Options;
+ cl::getRegisteredOptions(Options);
+ for (StringMap<cl::Option *>::iterator I = Options.begin(), E = Options.end();
+ I != E; ++I) {
+ if (I->second->Category != &Category && I->first() != "help" &&
+ I->first() != "version")
+ I->second->setHiddenFlag(cl::ReallyHidden);
+ }
Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc,
argv));
diff --git a/lib/Tooling/CompilationDatabase.cpp b/lib/Tooling/CompilationDatabase.cpp
index c962055..b513446 100644
--- a/lib/Tooling/CompilationDatabase.cpp
+++ b/lib/Tooling/CompilationDatabase.cpp
@@ -13,22 +13,21 @@
//===----------------------------------------------------------------------===//
#include "clang/Tooling/CompilationDatabase.h"
-#include "clang/Tooling/CompilationDatabasePluginRegistry.h"
-#include "clang/Tooling/Tooling.h"
-#include "llvm/ADT/SmallString.h"
-#include "llvm/Support/Path.h"
-#include "llvm/Support/system_error.h"
-#include <sstream>
-
#include "clang/Basic/Diagnostic.h"
#include "clang/Driver/Action.h"
+#include "clang/Driver/Compilation.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/Job.h"
-#include "clang/Driver/Compilation.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
-#include "llvm/Support/Host.h"
+#include "clang/Tooling/CompilationDatabasePluginRegistry.h"
+#include "clang/Tooling/Tooling.h"
+#include "llvm/ADT/SmallString.h"
#include "llvm/Option/Arg.h"
+#include "llvm/Support/Host.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/system_error.h"
+#include <sstream>
namespace clang {
namespace tooling {
@@ -44,7 +43,7 @@
Ie = CompilationDatabasePluginRegistry::end();
It != Ie; ++It) {
std::string DatabaseErrorMessage;
- OwningPtr<CompilationDatabasePlugin> Plugin(It->instantiate());
+ std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
if (CompilationDatabase *DB =
Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
return DB;
@@ -158,7 +157,7 @@
UnusedInputDiagConsumer(DiagnosticConsumer *Other) : Other(Other) {}
virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
- const Diagnostic &Info) LLVM_OVERRIDE {
+ const Diagnostic &Info) override {
if (Info.getID() == clang::diag::warn_drv_input_file_unused) {
// Arg 1 for this diagnostic is the option that didn't get used.
UnusedInputs.push_back(Info.getArgStdStr(0));
@@ -204,8 +203,8 @@
/// \li true if successful.
/// \li false if \c Args cannot be used for compilation jobs (e.g.
/// contains an option like -E or -version).
-bool stripPositionalArgs(std::vector<const char *> Args,
- std::vector<std::string> &Result) {
+static bool stripPositionalArgs(std::vector<const char *> Args,
+ std::vector<std::string> &Result) {
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
UnusedInputDiagConsumer DiagClient;
DiagnosticsEngine Diagnostics(
@@ -214,7 +213,7 @@
// Neither clang executable nor default image name are required since the
// jobs the driver builds will not be executed.
- OwningPtr<driver::Driver> NewDriver(new driver::Driver(
+ std::unique_ptr<driver::Driver> NewDriver(new driver::Driver(
/* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(),
/* DefaultImageName= */ "", Diagnostics));
NewDriver->setCheckInputsExist(false);
@@ -237,7 +236,12 @@
// up with no jobs but then this is the user's fault.
Args.push_back("placeholder.cpp");
- const OwningPtr<driver::Compilation> Compilation(
+ // Remove -no-integrated-as; it's not used for syntax checking,
+ // and it confuses targets which don't support this option.
+ std::remove_if(Args.begin(), Args.end(),
+ MatchesAny(std::string("-no-integrated-as")));
+
+ const std::unique_ptr<driver::Compilation> Compilation(
NewDriver->BuildCompilation(Args));
const driver::JobList &Jobs = Compilation->getJobs();
@@ -271,6 +275,7 @@
End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs));
// Remove the -c add above as well. It will be at the end right now.
+ assert(strcmp(*(End - 1), "-c") == 0);
--End;
Result = std::vector<std::string>(Args.begin() + 1, End);
@@ -298,7 +303,8 @@
std::vector<std::string> ToolCommandLine(1, "clang-tool");
ToolCommandLine.insert(ToolCommandLine.end(),
CommandLine.begin(), CommandLine.end());
- CompileCommands.push_back(CompileCommand(Directory, ToolCommandLine));
+ CompileCommands.push_back(
+ CompileCommand(Directory, std::move(ToolCommandLine)));
}
std::vector<CompileCommand>
diff --git a/lib/Tooling/FileMatchTrie.cpp b/lib/Tooling/FileMatchTrie.cpp
index 89979a8..dc9999e 100644
--- a/lib/Tooling/FileMatchTrie.cpp
+++ b/lib/Tooling/FileMatchTrie.cpp
@@ -24,7 +24,7 @@
/// \brief Default \c PathComparator using \c llvm::sys::fs::equivalent().
struct DefaultPathComparator : public PathComparator {
virtual ~DefaultPathComparator() {}
- virtual bool equivalent(StringRef FileA, StringRef FileB) const {
+ bool equivalent(StringRef FileA, StringRef FileB) const override {
return FileA == FileB || llvm::sys::fs::equivalent(FileA, FileB);
}
};
diff --git a/lib/Tooling/JSONCompilationDatabase.cpp b/lib/Tooling/JSONCompilationDatabase.cpp
index 1e33b41..5fe0980 100644
--- a/lib/Tooling/JSONCompilationDatabase.cpp
+++ b/lib/Tooling/JSONCompilationDatabase.cpp
@@ -118,15 +118,15 @@
}
class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin {
- virtual CompilationDatabase *loadFromDirectory(
- StringRef Directory, std::string &ErrorMessage) {
+ CompilationDatabase *loadFromDirectory(StringRef Directory,
+ std::string &ErrorMessage) override {
SmallString<1024> JSONDatabasePath(Directory);
llvm::sys::path::append(JSONDatabasePath, "compile_commands.json");
- OwningPtr<CompilationDatabase> Database(
+ std::unique_ptr<CompilationDatabase> Database(
JSONCompilationDatabase::loadFromFile(JSONDatabasePath, ErrorMessage));
if (!Database)
return NULL;
- return Database.take();
+ return Database.release();
}
};
@@ -144,37 +144,37 @@
JSONCompilationDatabase *
JSONCompilationDatabase::loadFromFile(StringRef FilePath,
std::string &ErrorMessage) {
- OwningPtr<llvm::MemoryBuffer> DatabaseBuffer;
+ std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer;
llvm::error_code Result =
llvm::MemoryBuffer::getFile(FilePath, DatabaseBuffer);
if (Result != 0) {
ErrorMessage = "Error while opening JSON database: " + Result.message();
return NULL;
}
- OwningPtr<JSONCompilationDatabase> Database(
- new JSONCompilationDatabase(DatabaseBuffer.take()));
+ std::unique_ptr<JSONCompilationDatabase> Database(
+ new JSONCompilationDatabase(DatabaseBuffer.release()));
if (!Database->parse(ErrorMessage))
return NULL;
- return Database.take();
+ return Database.release();
}
JSONCompilationDatabase *
JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString,
std::string &ErrorMessage) {
- OwningPtr<llvm::MemoryBuffer> DatabaseBuffer(
+ std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer(
llvm::MemoryBuffer::getMemBuffer(DatabaseString));
- OwningPtr<JSONCompilationDatabase> Database(
- new JSONCompilationDatabase(DatabaseBuffer.take()));
+ std::unique_ptr<JSONCompilationDatabase> Database(
+ new JSONCompilationDatabase(DatabaseBuffer.release()));
if (!Database->parse(ErrorMessage))
return NULL;
- return Database.take();
+ return Database.release();
}
std::vector<CompileCommand>
JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const {
SmallString<128> NativeFilePath;
llvm::sys::path::native(FilePath, NativeFilePath);
- std::vector<StringRef> PossibleMatches;
+
std::string Error;
llvm::raw_string_ostream ES(Error);
StringRef Match = MatchTrie.findEquivalent(NativeFilePath.str(), ES);
diff --git a/lib/Tooling/Refactoring.cpp b/lib/Tooling/Refactoring.cpp
index e165c12..df9600e 100644
--- a/lib/Tooling/Refactoring.cpp
+++ b/lib/Tooling/Refactoring.cpp
@@ -18,9 +18,9 @@
#include "clang/Lex/Lexer.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/Refactoring.h"
-#include "llvm/Support/raw_os_ostream.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
+#include "llvm/Support/raw_os_ostream.h"
namespace clang {
namespace tooling {
@@ -35,12 +35,13 @@
: FilePath(FilePath), ReplacementRange(Offset, Length),
ReplacementText(ReplacementText) {}
-Replacement::Replacement(SourceManager &Sources, SourceLocation Start,
+Replacement::Replacement(const SourceManager &Sources, SourceLocation Start,
unsigned Length, StringRef ReplacementText) {
setFromSourceLocation(Sources, Start, Length, ReplacementText);
}
-Replacement::Replacement(SourceManager &Sources, const CharSourceRange &Range,
+Replacement::Replacement(const SourceManager &Sources,
+ const CharSourceRange &Range,
StringRef ReplacementText) {
setFromSourceRange(Sources, Range, ReplacementText);
}
@@ -99,7 +100,7 @@
LHS.getReplacementText() == RHS.getReplacementText();
}
-void Replacement::setFromSourceLocation(SourceManager &Sources,
+void Replacement::setFromSourceLocation(const SourceManager &Sources,
SourceLocation Start, unsigned Length,
StringRef ReplacementText) {
const std::pair<FileID, unsigned> DecomposedLocation =
@@ -121,7 +122,8 @@
// FIXME: This should go into the Lexer, but we need to figure out how
// to handle ranges for refactoring in general first - there is no obvious
// good way how to integrate this into the Lexer yet.
-static int getRangeSize(SourceManager &Sources, const CharSourceRange &Range) {
+static int getRangeSize(const SourceManager &Sources,
+ const CharSourceRange &Range) {
SourceLocation SpellingBegin = Sources.getSpellingLoc(Range.getBegin());
SourceLocation SpellingEnd = Sources.getSpellingLoc(Range.getEnd());
std::pair<FileID, unsigned> Start = Sources.getDecomposedLoc(SpellingBegin);
@@ -133,7 +135,7 @@
return End.second - Start.second;
}
-void Replacement::setFromSourceRange(SourceManager &Sources,
+void Replacement::setFromSourceRange(const SourceManager &Sources,
const CharSourceRange &Range,
StringRef ReplacementText) {
setFromSourceLocation(Sources, Sources.getSpellingLoc(Range.getBegin()),
diff --git a/lib/Tooling/Tooling.cpp b/lib/Tooling/Tooling.cpp
index a58854d..7425e3b 100644
--- a/lib/Tooling/Tooling.cpp
+++ b/lib/Tooling/Tooling.cpp
@@ -24,6 +24,7 @@
#include "clang/Tooling/ArgumentsAdjusters.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/Config/config.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
@@ -31,7 +32,7 @@
#include "llvm/Support/raw_ostream.h"
// For chdir, see the comment in ClangTool::run for more information.
-#ifdef _WIN32
+#ifdef LLVM_ON_WIN32
# include <direct.h>
#else
# include <unistd.h>
@@ -51,7 +52,7 @@
/// \brief Builds a clang driver initialized for running clang tools.
static clang::driver::Driver *newDriver(clang::DiagnosticsEngine *Diagnostics,
const char *BinaryName) {
- const std::string DefaultOutputName = "a.out";
+ const char *DefaultOutputName = "a.out";
clang::driver::Driver *CompilerDriver = new clang::driver::Driver(
BinaryName, llvm::sys::getDefaultTargetTriple(),
DefaultOutputName, *Diagnostics);
@@ -99,6 +100,7 @@
*Diagnostics);
Invocation->getFrontendOpts().DisableFree = false;
Invocation->getCodeGenOpts().DisableFree = false;
+ Invocation->getDependencyOutputOpts() = DependencyOutputOptions();
return Invocation;
}
@@ -158,22 +160,22 @@
public:
SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {}
- FrontendAction *create() { return Action; }
+ FrontendAction *create() override { return Action; }
};
}
-ToolInvocation::ToolInvocation(ArrayRef<std::string> CommandLine,
+ToolInvocation::ToolInvocation(std::vector<std::string> CommandLine,
ToolAction *Action, FileManager *Files)
- : CommandLine(CommandLine.vec()),
+ : CommandLine(std::move(CommandLine)),
Action(Action),
OwnsAction(false),
Files(Files),
DiagConsumer(NULL) {}
-ToolInvocation::ToolInvocation(ArrayRef<std::string> CommandLine,
+ToolInvocation::ToolInvocation(std::vector<std::string> CommandLine,
FrontendAction *FAction, FileManager *Files)
- : CommandLine(CommandLine.vec()),
+ : CommandLine(std::move(CommandLine)),
Action(new SingleFrontendActionFactory(FAction)),
OwnsAction(true),
Files(Files),
@@ -196,8 +198,8 @@
bool ToolInvocation::run() {
std::vector<const char*> Argv;
- for (int I = 0, E = CommandLine.size(); I != E; ++I)
- Argv.push_back(CommandLine[I].c_str());
+ for (const std::string &Str : CommandLine)
+ Argv.push_back(Str.c_str());
const char *const BinaryName = Argv[0];
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
TextDiagnosticPrinter DiagnosticPrinter(
@@ -206,28 +208,25 @@
IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false);
- const OwningPtr<clang::driver::Driver> Driver(
+ const std::unique_ptr<clang::driver::Driver> Driver(
newDriver(&Diagnostics, BinaryName));
// Since the input might only be virtual, don't check whether it exists.
Driver->setCheckInputsExist(false);
- const OwningPtr<clang::driver::Compilation> Compilation(
+ const std::unique_ptr<clang::driver::Compilation> Compilation(
Driver->BuildCompilation(llvm::makeArrayRef(Argv)));
const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments(
&Diagnostics, Compilation.get());
if (CC1Args == NULL) {
return false;
}
- OwningPtr<clang::CompilerInvocation> Invocation(
+ std::unique_ptr<clang::CompilerInvocation> Invocation(
newInvocation(&Diagnostics, *CC1Args));
- for (llvm::StringMap<StringRef>::const_iterator
- It = MappedFileContents.begin(), End = MappedFileContents.end();
- It != End; ++It) {
+ for (const auto &It : MappedFileContents) {
// Inject the code as the given file name into the preprocessor options.
- const llvm::MemoryBuffer *Input =
- llvm::MemoryBuffer::getMemBuffer(It->getValue());
- Invocation->getPreprocessorOpts().addRemappedFile(It->getKey(), Input);
+ auto *Input = llvm::MemoryBuffer::getMemBuffer(It.getValue());
+ Invocation->getPreprocessorOpts().addRemappedFile(It.getKey(), Input);
}
- return runInvocation(BinaryName, Compilation.get(), Invocation.take());
+ return runInvocation(BinaryName, Compilation.get(), Invocation.release());
}
bool ToolInvocation::runInvocation(
@@ -254,8 +253,8 @@
// The FrontendAction can have lifetime requirements for Compiler or its
// members, and we need to ensure it's deleted earlier than Compiler. So we
- // pass it to an OwningPtr declared after the Compiler variable.
- OwningPtr<FrontendAction> ScopedToolAction(create());
+ // pass it to an std::unique_ptr declared after the Compiler variable.
+ std::unique_ptr<FrontendAction> ScopedToolAction(create());
// Create the compilers actual diagnostics engine.
Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
@@ -275,15 +274,15 @@
: Files(new FileManager(FileSystemOptions())), DiagConsumer(NULL) {
ArgsAdjusters.push_back(new ClangStripOutputAdjuster());
ArgsAdjusters.push_back(new ClangSyntaxOnlyAdjuster());
- for (unsigned I = 0, E = SourcePaths.size(); I != E; ++I) {
- SmallString<1024> File(getAbsolutePath(SourcePaths[I]));
+ for (const auto &SourcePath : SourcePaths) {
+ std::string File(getAbsolutePath(SourcePath));
std::vector<CompileCommand> CompileCommandsForFile =
- Compilations.getCompileCommands(File.str());
+ Compilations.getCompileCommands(File);
if (!CompileCommandsForFile.empty()) {
- for (int I = 0, E = CompileCommandsForFile.size(); I != E; ++I) {
- CompileCommands.push_back(std::make_pair(File.str(),
- CompileCommandsForFile[I]));
+ for (CompileCommand &CompileCommand : CompileCommandsForFile) {
+ CompileCommands.push_back(
+ std::make_pair(File, std::move(CompileCommand)));
}
} else {
// FIXME: There are two use cases here: doing a fuzzy
@@ -332,8 +331,7 @@
llvm::sys::fs::getMainExecutable("clang_tool", &StaticSymbol);
bool ProcessingFailed = false;
- for (unsigned I = 0; I < CompileCommands.size(); ++I) {
- std::string File = CompileCommands[I].first;
+ for (const auto &Command : CompileCommands) {
// FIXME: chdir is thread hostile; on the other hand, creating the same
// behavior as chdir is complex: chdir resolves the path once, thus
// guaranteeing that all subsequent relative path operations work
@@ -341,28 +339,27 @@
// for example on network filesystems, where symlinks might be switched
// during runtime of the tool. Fixing this depends on having a file system
// abstraction that allows openat() style interactions.
- if (chdir(CompileCommands[I].second.Directory.c_str()))
+ if (chdir(Command.second.Directory.c_str()))
llvm::report_fatal_error("Cannot chdir into \"" +
- CompileCommands[I].second.Directory + "\n!");
- std::vector<std::string> CommandLine = CompileCommands[I].second.CommandLine;
- for (unsigned I = 0, E = ArgsAdjusters.size(); I != E; ++I)
- CommandLine = ArgsAdjusters[I]->Adjust(CommandLine);
+ Twine(Command.second.Directory) + "\n!");
+ std::vector<std::string> CommandLine = Command.second.CommandLine;
+ for (ArgumentsAdjuster *Adjuster : ArgsAdjusters)
+ CommandLine = Adjuster->Adjust(CommandLine);
assert(!CommandLine.empty());
CommandLine[0] = MainExecutable;
// FIXME: We need a callback mechanism for the tool writer to output a
// customized message for each file.
DEBUG({
- llvm::dbgs() << "Processing: " << File << ".\n";
+ llvm::dbgs() << "Processing: " << Command.first << ".\n";
});
- ToolInvocation Invocation(CommandLine, Action, Files.getPtr());
+ ToolInvocation Invocation(std::move(CommandLine), Action, Files.getPtr());
Invocation.setDiagnosticConsumer(DiagConsumer);
- for (int I = 0, E = MappedFileContents.size(); I != E; ++I) {
- Invocation.mapVirtualFile(MappedFileContents[I].first,
- MappedFileContents[I].second);
+ for (const auto &MappedFile : MappedFileContents) {
+ Invocation.mapVirtualFile(MappedFile.first, MappedFile.second);
}
if (!Invocation.run()) {
// FIXME: Diagnostics should be used instead.
- llvm::errs() << "Error while processing " << File << ".\n";
+ llvm::errs() << "Error while processing " << Command.first << ".\n";
ProcessingFailed = true;
}
}
@@ -378,7 +375,7 @@
ASTBuilderAction(std::vector<ASTUnit *> &ASTs) : ASTs(ASTs) {}
bool runInvocation(CompilerInvocation *Invocation, FileManager *Files,
- DiagnosticConsumer *DiagConsumer) {
+ DiagnosticConsumer *DiagConsumer) override {
// FIXME: This should use the provided FileManager.
ASTUnit *AST = ASTUnit::LoadFromCompilerInvocation(
Invocation, CompilerInstance::createDiagnostics(