2012-08-24 13:50:27 +08:00
|
|
|
//===--- JSONCompilationDatabase.cpp - ------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the implementation of the JSONCompilationDatabase.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Tooling/JSONCompilationDatabase.h"
|
|
|
|
#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"
|
2014-06-13 01:19:42 +08:00
|
|
|
#include <system_error>
|
2012-08-24 13:50:27 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tooling {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief A parser for escaped strings of command line arguments.
|
|
|
|
///
|
|
|
|
/// Assumes \-escaping for quoted arguments (see the documentation of
|
|
|
|
/// unescapeCommandLine(...)).
|
|
|
|
class CommandLineArgumentParser {
|
|
|
|
public:
|
|
|
|
CommandLineArgumentParser(StringRef CommandLine)
|
|
|
|
: Input(CommandLine), Position(Input.begin()-1) {}
|
|
|
|
|
|
|
|
std::vector<std::string> parse() {
|
|
|
|
bool HasMoreInput = true;
|
|
|
|
while (HasMoreInput && nextNonWhitespace()) {
|
|
|
|
std::string Argument;
|
|
|
|
HasMoreInput = parseStringInto(Argument);
|
|
|
|
CommandLine.push_back(Argument);
|
|
|
|
}
|
|
|
|
return CommandLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// All private methods return true if there is more input available.
|
|
|
|
|
|
|
|
bool parseStringInto(std::string &String) {
|
|
|
|
do {
|
|
|
|
if (*Position == '"') {
|
2013-03-02 14:00:16 +08:00
|
|
|
if (!parseDoubleQuotedStringInto(String)) return false;
|
|
|
|
} else if (*Position == '\'') {
|
|
|
|
if (!parseSingleQuotedStringInto(String)) return false;
|
2012-08-24 13:50:27 +08:00
|
|
|
} else {
|
|
|
|
if (!parseFreeStringInto(String)) return false;
|
|
|
|
}
|
|
|
|
} while (*Position != ' ');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-02 14:00:16 +08:00
|
|
|
bool parseDoubleQuotedStringInto(std::string &String) {
|
2012-08-24 13:50:27 +08:00
|
|
|
if (!next()) return false;
|
|
|
|
while (*Position != '"') {
|
|
|
|
if (!skipEscapeCharacter()) return false;
|
|
|
|
String.push_back(*Position);
|
|
|
|
if (!next()) return false;
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2013-03-02 14:00:16 +08:00
|
|
|
bool parseSingleQuotedStringInto(std::string &String) {
|
|
|
|
if (!next()) return false;
|
|
|
|
while (*Position != '\'') {
|
|
|
|
String.push_back(*Position);
|
|
|
|
if (!next()) return false;
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2012-08-24 13:50:27 +08:00
|
|
|
bool parseFreeStringInto(std::string &String) {
|
|
|
|
do {
|
|
|
|
if (!skipEscapeCharacter()) return false;
|
|
|
|
String.push_back(*Position);
|
|
|
|
if (!next()) return false;
|
2013-03-02 14:00:16 +08:00
|
|
|
} while (*Position != ' ' && *Position != '"' && *Position != '\'');
|
2012-08-24 13:50:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool skipEscapeCharacter() {
|
|
|
|
if (*Position == '\\') {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nextNonWhitespace() {
|
|
|
|
do {
|
|
|
|
if (!next()) return false;
|
|
|
|
} while (*Position == ' ');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool next() {
|
|
|
|
++Position;
|
|
|
|
return Position != Input.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const StringRef Input;
|
|
|
|
StringRef::iterator Position;
|
|
|
|
std::vector<std::string> CommandLine;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> unescapeCommandLine(
|
|
|
|
StringRef EscapedCommandLine) {
|
|
|
|
CommandLineArgumentParser parser(EscapedCommandLine);
|
|
|
|
return parser.parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin {
|
2014-08-09 00:06:15 +08:00
|
|
|
std::unique_ptr<CompilationDatabase>
|
|
|
|
loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override {
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<1024> JSONDatabasePath(Directory);
|
2012-08-24 13:50:27 +08:00
|
|
|
llvm::sys::path::append(JSONDatabasePath, "compile_commands.json");
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<CompilationDatabase> Database(
|
2012-08-24 13:50:27 +08:00
|
|
|
JSONCompilationDatabase::loadFromFile(JSONDatabasePath, ErrorMessage));
|
|
|
|
if (!Database)
|
2014-05-20 12:51:16 +08:00
|
|
|
return nullptr;
|
2014-08-09 00:06:15 +08:00
|
|
|
return Database;
|
2012-08-24 13:50:27 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-01 14:34:58 +08:00
|
|
|
} // end namespace
|
|
|
|
|
2012-08-24 13:50:27 +08:00
|
|
|
// Register the JSONCompilationDatabasePlugin with the
|
|
|
|
// CompilationDatabasePluginRegistry using this statically initialized variable.
|
|
|
|
static CompilationDatabasePluginRegistry::Add<JSONCompilationDatabasePlugin>
|
|
|
|
X("json-compilation-database", "Reads JSON formatted compilation databases");
|
|
|
|
|
|
|
|
// This anchor is used to force the linker to link in the generated object file
|
|
|
|
// and thus register the JSONCompilationDatabasePlugin.
|
2012-08-24 18:39:28 +08:00
|
|
|
volatile int JSONAnchorSource = 0;
|
2012-08-24 13:50:27 +08:00
|
|
|
|
2014-08-09 00:06:15 +08:00
|
|
|
std::unique_ptr<JSONCompilationDatabase>
|
2012-08-24 13:50:27 +08:00
|
|
|
JSONCompilationDatabase::loadFromFile(StringRef FilePath,
|
|
|
|
std::string &ErrorMessage) {
|
2014-07-07 01:43:24 +08:00
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> DatabaseBuffer =
|
|
|
|
llvm::MemoryBuffer::getFile(FilePath);
|
|
|
|
if (std::error_code Result = DatabaseBuffer.getError()) {
|
2012-08-24 13:50:27 +08:00
|
|
|
ErrorMessage = "Error while opening JSON database: " + Result.message();
|
2014-05-20 12:51:16 +08:00
|
|
|
return nullptr;
|
2012-08-24 13:50:27 +08:00
|
|
|
}
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<JSONCompilationDatabase> Database(
|
2014-08-09 06:01:06 +08:00
|
|
|
new JSONCompilationDatabase(std::move(*DatabaseBuffer)));
|
2012-08-24 13:50:27 +08:00
|
|
|
if (!Database->parse(ErrorMessage))
|
2014-05-20 12:51:16 +08:00
|
|
|
return nullptr;
|
2014-08-09 00:06:15 +08:00
|
|
|
return Database;
|
2012-08-24 13:50:27 +08:00
|
|
|
}
|
|
|
|
|
2014-08-09 00:06:15 +08:00
|
|
|
std::unique_ptr<JSONCompilationDatabase>
|
2012-08-24 13:50:27 +08:00
|
|
|
JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString,
|
|
|
|
std::string &ErrorMessage) {
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer(
|
2012-08-24 13:50:27 +08:00
|
|
|
llvm::MemoryBuffer::getMemBuffer(DatabaseString));
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<JSONCompilationDatabase> Database(
|
2014-08-09 06:01:06 +08:00
|
|
|
new JSONCompilationDatabase(std::move(DatabaseBuffer)));
|
2012-08-24 13:50:27 +08:00
|
|
|
if (!Database->parse(ErrorMessage))
|
2014-05-20 12:51:16 +08:00
|
|
|
return nullptr;
|
2014-08-09 00:06:15 +08:00
|
|
|
return Database;
|
2012-08-24 13:50:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CompileCommand>
|
|
|
|
JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const {
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<128> NativeFilePath;
|
2012-08-24 13:50:27 +08:00
|
|
|
llvm::sys::path::native(FilePath, NativeFilePath);
|
2013-11-27 13:22:15 +08:00
|
|
|
|
2012-10-09 00:08:15 +08:00
|
|
|
std::string Error;
|
|
|
|
llvm::raw_string_ostream ES(Error);
|
2015-03-18 18:17:07 +08:00
|
|
|
StringRef Match = MatchTrie.findEquivalent(NativeFilePath, ES);
|
2013-01-13 02:37:52 +08:00
|
|
|
if (Match.empty())
|
2012-10-09 00:08:15 +08:00
|
|
|
return std::vector<CompileCommand>();
|
2012-08-24 13:50:27 +08:00
|
|
|
llvm::StringMap< std::vector<CompileCommandRef> >::const_iterator
|
2012-10-09 00:08:15 +08:00
|
|
|
CommandsRefI = IndexByFile.find(Match);
|
2012-08-24 13:50:27 +08:00
|
|
|
if (CommandsRefI == IndexByFile.end())
|
|
|
|
return std::vector<CompileCommand>();
|
|
|
|
std::vector<CompileCommand> Commands;
|
2012-12-04 15:26:44 +08:00
|
|
|
getCommands(CommandsRefI->getValue(), Commands);
|
2012-08-24 13:50:27 +08:00
|
|
|
return Commands;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
JSONCompilationDatabase::getAllFiles() const {
|
|
|
|
std::vector<std::string> Result;
|
|
|
|
|
|
|
|
llvm::StringMap< std::vector<CompileCommandRef> >::const_iterator
|
|
|
|
CommandsRefI = IndexByFile.begin();
|
|
|
|
const llvm::StringMap< std::vector<CompileCommandRef> >::const_iterator
|
|
|
|
CommandsRefEnd = IndexByFile.end();
|
|
|
|
for (; CommandsRefI != CommandsRefEnd; ++CommandsRefI) {
|
|
|
|
Result.push_back(CommandsRefI->first().str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2012-12-04 15:26:44 +08:00
|
|
|
std::vector<CompileCommand>
|
|
|
|
JSONCompilationDatabase::getAllCompileCommands() const {
|
|
|
|
std::vector<CompileCommand> Commands;
|
|
|
|
for (llvm::StringMap< std::vector<CompileCommandRef> >::const_iterator
|
|
|
|
CommandsRefI = IndexByFile.begin(), CommandsRefEnd = IndexByFile.end();
|
|
|
|
CommandsRefI != CommandsRefEnd; ++CommandsRefI) {
|
|
|
|
getCommands(CommandsRefI->getValue(), Commands);
|
|
|
|
}
|
|
|
|
return Commands;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSONCompilationDatabase::getCommands(
|
|
|
|
ArrayRef<CompileCommandRef> CommandsRef,
|
|
|
|
std::vector<CompileCommand> &Commands) const {
|
|
|
|
for (int I = 0, E = CommandsRef.size(); I != E; ++I) {
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<8> DirectoryStorage;
|
|
|
|
SmallString<1024> CommandStorage;
|
2015-05-30 03:42:19 +08:00
|
|
|
Commands.emplace_back(
|
|
|
|
// FIXME: Escape correctly:
|
|
|
|
CommandsRef[I].first->getValue(DirectoryStorage),
|
|
|
|
unescapeCommandLine(CommandsRef[I].second->getValue(CommandStorage)));
|
2012-12-04 15:26:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 13:50:27 +08:00
|
|
|
bool JSONCompilationDatabase::parse(std::string &ErrorMessage) {
|
|
|
|
llvm::yaml::document_iterator I = YAMLStream.begin();
|
|
|
|
if (I == YAMLStream.end()) {
|
|
|
|
ErrorMessage = "Error while parsing YAML.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llvm::yaml::Node *Root = I->getRoot();
|
2014-05-20 12:51:16 +08:00
|
|
|
if (!Root) {
|
2012-08-24 13:50:27 +08:00
|
|
|
ErrorMessage = "Error while parsing YAML.";
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-13 03:30:44 +08:00
|
|
|
llvm::yaml::SequenceNode *Array = dyn_cast<llvm::yaml::SequenceNode>(Root);
|
2014-05-20 12:51:16 +08:00
|
|
|
if (!Array) {
|
2012-08-24 13:50:27 +08:00
|
|
|
ErrorMessage = "Expected array.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (llvm::yaml::SequenceNode::iterator AI = Array->begin(),
|
|
|
|
AE = Array->end();
|
|
|
|
AI != AE; ++AI) {
|
2013-01-13 03:30:44 +08:00
|
|
|
llvm::yaml::MappingNode *Object = dyn_cast<llvm::yaml::MappingNode>(&*AI);
|
2014-05-20 12:51:16 +08:00
|
|
|
if (!Object) {
|
2012-08-24 13:50:27 +08:00
|
|
|
ErrorMessage = "Expected object.";
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-20 12:51:16 +08:00
|
|
|
llvm::yaml::ScalarNode *Directory = nullptr;
|
|
|
|
llvm::yaml::ScalarNode *Command = nullptr;
|
|
|
|
llvm::yaml::ScalarNode *File = nullptr;
|
2012-08-24 13:50:27 +08:00
|
|
|
for (llvm::yaml::MappingNode::iterator KVI = Object->begin(),
|
|
|
|
KVE = Object->end();
|
|
|
|
KVI != KVE; ++KVI) {
|
|
|
|
llvm::yaml::Node *Value = (*KVI).getValue();
|
2014-05-20 12:51:16 +08:00
|
|
|
if (!Value) {
|
2012-08-24 13:50:27 +08:00
|
|
|
ErrorMessage = "Expected value.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llvm::yaml::ScalarNode *ValueString =
|
2013-01-13 03:30:44 +08:00
|
|
|
dyn_cast<llvm::yaml::ScalarNode>(Value);
|
2014-05-20 12:51:16 +08:00
|
|
|
if (!ValueString) {
|
2012-08-24 13:50:27 +08:00
|
|
|
ErrorMessage = "Expected string as value.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llvm::yaml::ScalarNode *KeyString =
|
2013-01-13 03:30:44 +08:00
|
|
|
dyn_cast<llvm::yaml::ScalarNode>((*KVI).getKey());
|
2014-05-20 12:51:16 +08:00
|
|
|
if (!KeyString) {
|
2012-08-24 13:50:27 +08:00
|
|
|
ErrorMessage = "Expected strings as key.";
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<8> KeyStorage;
|
2012-08-24 13:50:27 +08:00
|
|
|
if (KeyString->getValue(KeyStorage) == "directory") {
|
|
|
|
Directory = ValueString;
|
|
|
|
} else if (KeyString->getValue(KeyStorage) == "command") {
|
|
|
|
Command = ValueString;
|
|
|
|
} else if (KeyString->getValue(KeyStorage) == "file") {
|
|
|
|
File = ValueString;
|
|
|
|
} else {
|
|
|
|
ErrorMessage = ("Unknown key: \"" +
|
|
|
|
KeyString->getRawValue() + "\"").str();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!File) {
|
|
|
|
ErrorMessage = "Missing key: \"file\".";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!Command) {
|
|
|
|
ErrorMessage = "Missing key: \"command\".";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!Directory) {
|
|
|
|
ErrorMessage = "Missing key: \"directory\".";
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<8> FileStorage;
|
2012-10-09 00:08:15 +08:00
|
|
|
StringRef FileName = File->getValue(FileStorage);
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<128> NativeFilePath;
|
2012-10-09 00:08:15 +08:00
|
|
|
if (llvm::sys::path::is_relative(FileName)) {
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<8> DirectoryStorage;
|
|
|
|
SmallString<128> AbsolutePath(
|
2012-10-09 00:08:15 +08:00
|
|
|
Directory->getValue(DirectoryStorage));
|
|
|
|
llvm::sys::path::append(AbsolutePath, FileName);
|
2015-03-18 18:17:07 +08:00
|
|
|
llvm::sys::path::native(AbsolutePath, NativeFilePath);
|
2012-10-09 00:08:15 +08:00
|
|
|
} else {
|
|
|
|
llvm::sys::path::native(FileName, NativeFilePath);
|
|
|
|
}
|
2012-08-24 13:50:27 +08:00
|
|
|
IndexByFile[NativeFilePath].push_back(
|
2012-10-09 00:08:15 +08:00
|
|
|
CompileCommandRef(Directory, Command));
|
2015-03-18 18:17:07 +08:00
|
|
|
MatchTrie.insert(NativeFilePath);
|
2012-08-24 13:50:27 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace tooling
|
|
|
|
} // end namespace clang
|