blob: f8993658da35711a00445cd4f5d48244eb0b84d8 [file] [log] [blame]
Manuel Klimekcb971c62012-04-04 12:07:46 +00001//===--- CompilationDatabase.cpp - ----------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains multiple implementations for CompilationDatabases.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Tooling/CompilationDatabase.h"
15#include "llvm/ADT/SmallString.h"
Manuel Klimekc661f142012-04-17 16:54:26 +000016#include "llvm/Support/YAMLParser.h"
Manuel Klimekcb971c62012-04-04 12:07:46 +000017#include "llvm/Support/Path.h"
18#include "llvm/Support/system_error.h"
19
20namespace clang {
21namespace tooling {
22
23namespace {
24
Manuel Klimekc661f142012-04-17 16:54:26 +000025/// \brief A parser for escaped strings of command line arguments.
Manuel Klimekcb971c62012-04-04 12:07:46 +000026///
27/// Assumes \-escaping for quoted arguments (see the documentation of
Manuel Klimekc661f142012-04-17 16:54:26 +000028/// unescapeCommandLine(...)).
Manuel Klimekcb971c62012-04-04 12:07:46 +000029class CommandLineArgumentParser {
30 public:
31 CommandLineArgumentParser(StringRef CommandLine)
32 : Input(CommandLine), Position(Input.begin()-1) {}
33
34 std::vector<std::string> parse() {
35 bool HasMoreInput = true;
36 while (HasMoreInput && nextNonWhitespace()) {
37 std::string Argument;
38 HasMoreInput = parseStringInto(Argument);
39 CommandLine.push_back(Argument);
40 }
41 return CommandLine;
42 }
43
44 private:
45 // All private methods return true if there is more input available.
46
47 bool parseStringInto(std::string &String) {
48 do {
49 if (*Position == '"') {
50 if (!parseQuotedStringInto(String)) return false;
51 } else {
52 if (!parseFreeStringInto(String)) return false;
53 }
54 } while (*Position != ' ');
55 return true;
56 }
57
58 bool parseQuotedStringInto(std::string &String) {
59 if (!next()) return false;
60 while (*Position != '"') {
61 if (!skipEscapeCharacter()) return false;
62 String.push_back(*Position);
63 if (!next()) return false;
64 }
65 return next();
66 }
67
68 bool parseFreeStringInto(std::string &String) {
69 do {
70 if (!skipEscapeCharacter()) return false;
71 String.push_back(*Position);
72 if (!next()) return false;
73 } while (*Position != ' ' && *Position != '"');
74 return true;
75 }
76
77 bool skipEscapeCharacter() {
78 if (*Position == '\\') {
79 return next();
80 }
81 return true;
82 }
83
84 bool nextNonWhitespace() {
85 do {
86 if (!next()) return false;
87 } while (*Position == ' ');
88 return true;
89 }
90
91 bool next() {
92 ++Position;
Manuel Klimekcb971c62012-04-04 12:07:46 +000093 return Position != Input.end();
94 }
95
96 const StringRef Input;
97 StringRef::iterator Position;
98 std::vector<std::string> CommandLine;
99};
100
Manuel Klimekc661f142012-04-17 16:54:26 +0000101std::vector<std::string> unescapeCommandLine(
102 StringRef EscapedCommandLine) {
103 CommandLineArgumentParser parser(EscapedCommandLine);
Manuel Klimekcb971c62012-04-04 12:07:46 +0000104 return parser.parse();
105}
106
107} // end namespace
108
109CompilationDatabase::~CompilationDatabase() {}
110
111CompilationDatabase *
112CompilationDatabase::loadFromDirectory(StringRef BuildDirectory,
113 std::string &ErrorMessage) {
114 llvm::SmallString<1024> JSONDatabasePath(BuildDirectory);
115 llvm::sys::path::append(JSONDatabasePath, "compile_commands.json");
116 llvm::OwningPtr<CompilationDatabase> Database(
117 JSONCompilationDatabase::loadFromFile(JSONDatabasePath, ErrorMessage));
118 if (!Database) {
119 return NULL;
120 }
121 return Database.take();
122}
123
124JSONCompilationDatabase *
125JSONCompilationDatabase::loadFromFile(StringRef FilePath,
126 std::string &ErrorMessage) {
127 llvm::OwningPtr<llvm::MemoryBuffer> DatabaseBuffer;
128 llvm::error_code Result =
129 llvm::MemoryBuffer::getFile(FilePath, DatabaseBuffer);
130 if (Result != 0) {
131 ErrorMessage = "Error while opening JSON database: " + Result.message();
132 return NULL;
133 }
134 llvm::OwningPtr<JSONCompilationDatabase> Database(
135 new JSONCompilationDatabase(DatabaseBuffer.take()));
136 if (!Database->parse(ErrorMessage))
137 return NULL;
138 return Database.take();
139}
140
141JSONCompilationDatabase *
142JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString,
143 std::string &ErrorMessage) {
144 llvm::OwningPtr<llvm::MemoryBuffer> DatabaseBuffer(
145 llvm::MemoryBuffer::getMemBuffer(DatabaseString));
146 llvm::OwningPtr<JSONCompilationDatabase> Database(
147 new JSONCompilationDatabase(DatabaseBuffer.take()));
148 if (!Database->parse(ErrorMessage))
149 return NULL;
150 return Database.take();
151}
152
153std::vector<CompileCommand>
154JSONCompilationDatabase::getCompileCommands(StringRef FilePath) const {
155 llvm::StringMap< std::vector<CompileCommandRef> >::const_iterator
156 CommandsRefI = IndexByFile.find(FilePath);
157 if (CommandsRefI == IndexByFile.end())
158 return std::vector<CompileCommand>();
159 const std::vector<CompileCommandRef> &CommandsRef = CommandsRefI->getValue();
160 std::vector<CompileCommand> Commands;
161 for (int I = 0, E = CommandsRef.size(); I != E; ++I) {
Manuel Klimekc661f142012-04-17 16:54:26 +0000162 llvm::SmallString<8> DirectoryStorage;
163 llvm::SmallString<1024> CommandStorage;
Manuel Klimekcb971c62012-04-04 12:07:46 +0000164 Commands.push_back(CompileCommand(
165 // FIXME: Escape correctly:
Manuel Klimekc661f142012-04-17 16:54:26 +0000166 CommandsRef[I].first->getValue(DirectoryStorage),
167 unescapeCommandLine(CommandsRef[I].second->getValue(CommandStorage))));
Manuel Klimekcb971c62012-04-04 12:07:46 +0000168 }
169 return Commands;
170}
171
172bool JSONCompilationDatabase::parse(std::string &ErrorMessage) {
Manuel Klimekc661f142012-04-17 16:54:26 +0000173 llvm::yaml::document_iterator I = YAMLStream.begin();
174 if (I == YAMLStream.end()) {
175 ErrorMessage = "Error while parsing YAML.";
Manuel Klimekcb971c62012-04-04 12:07:46 +0000176 return false;
177 }
Manuel Klimekc661f142012-04-17 16:54:26 +0000178 llvm::yaml::Node *Root = I->getRoot();
179 if (Root == NULL) {
180 ErrorMessage = "Error while parsing YAML.";
181 return false;
182 }
183 llvm::yaml::SequenceNode *Array =
184 llvm::dyn_cast<llvm::yaml::SequenceNode>(Root);
Manuel Klimekcb971c62012-04-04 12:07:46 +0000185 if (Array == NULL) {
186 ErrorMessage = "Expected array.";
187 return false;
188 }
Manuel Klimekc661f142012-04-17 16:54:26 +0000189 for (llvm::yaml::SequenceNode::iterator AI = Array->begin(),
190 AE = Array->end();
Manuel Klimekcb971c62012-04-04 12:07:46 +0000191 AI != AE; ++AI) {
Manuel Klimekc661f142012-04-17 16:54:26 +0000192 llvm::yaml::MappingNode *Object =
193 llvm::dyn_cast<llvm::yaml::MappingNode>(&*AI);
Manuel Klimekcb971c62012-04-04 12:07:46 +0000194 if (Object == NULL) {
195 ErrorMessage = "Expected object.";
196 return false;
197 }
Manuel Klimekc661f142012-04-17 16:54:26 +0000198 llvm::yaml::ScalarNode *Directory;
199 llvm::yaml::ScalarNode *Command;
200 llvm::SmallString<8> FileStorage;
201 llvm::StringRef File;
202 for (llvm::yaml::MappingNode::iterator KVI = Object->begin(),
203 KVE = Object->end();
Manuel Klimekcb971c62012-04-04 12:07:46 +0000204 KVI != KVE; ++KVI) {
Manuel Klimekc661f142012-04-17 16:54:26 +0000205 llvm::yaml::Node *Value = (*KVI).getValue();
Manuel Klimekcb971c62012-04-04 12:07:46 +0000206 if (Value == NULL) {
207 ErrorMessage = "Expected value.";
208 return false;
209 }
Manuel Klimekc661f142012-04-17 16:54:26 +0000210 llvm::yaml::ScalarNode *ValueString =
211 llvm::dyn_cast<llvm::yaml::ScalarNode>(Value);
Manuel Klimekcb971c62012-04-04 12:07:46 +0000212 if (ValueString == NULL) {
213 ErrorMessage = "Expected string as value.";
214 return false;
215 }
Manuel Klimekc661f142012-04-17 16:54:26 +0000216 llvm::yaml::ScalarNode *KeyString =
217 llvm::dyn_cast<llvm::yaml::ScalarNode>((*KVI).getKey());
218 llvm::SmallString<8> KeyStorage;
219 if (KeyString->getValue(KeyStorage) == "directory") {
220 Directory = ValueString;
221 } else if (KeyString->getValue(KeyStorage) == "command") {
222 Command = ValueString;
223 } else if (KeyString->getValue(KeyStorage) == "file") {
224 File = ValueString->getValue(FileStorage);
Manuel Klimekcb971c62012-04-04 12:07:46 +0000225 } else {
Manuel Klimekc661f142012-04-17 16:54:26 +0000226 ErrorMessage = ("Unknown key: \"" +
227 KeyString->getRawValue() + "\"").str();
Manuel Klimekcb971c62012-04-04 12:07:46 +0000228 return false;
229 }
230 }
Manuel Klimekc661f142012-04-17 16:54:26 +0000231 IndexByFile[File].push_back(
232 CompileCommandRef(Directory, Command));
Manuel Klimekcb971c62012-04-04 12:07:46 +0000233 }
234 return true;
235}
236
237} // end namespace tooling
238} // end namespace clang
239