Skip to content

Commit 650e04e

Browse files
committed
[Tooling] JSONCompilationDatabase::loadFromBuffer retains the buffer, copy it.
This function doesn't seem to be used in-tree outside tests. However clangd wants to use it soon, and having the CDB be self-contained seems reasonable. Differential Revision: https://reviews.llvm.org/D92646
1 parent d10f986 commit 650e04e

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

clang/lib/Tooling/JSONCompilationDatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString,
217217
std::string &ErrorMessage,
218218
JSONCommandLineSyntax Syntax) {
219219
std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer(
220-
llvm::MemoryBuffer::getMemBuffer(DatabaseString));
220+
llvm::MemoryBuffer::getMemBufferCopy(DatabaseString));
221221
std::unique_ptr<JSONCompilationDatabase> Database(
222222
new JSONCompilationDatabase(std::move(DatabaseBuffer), Syntax));
223223
if (!Database->parse(ErrorMessage))

clang/unittests/Tooling/CompilationDatabaseTest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,15 @@ TEST(JSONCompilationDatabase, GetAllCompileCommands) {
172172
}
173173

174174
static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName,
175-
StringRef JSONDatabase,
175+
std::string JSONDatabase,
176176
std::string &ErrorMessage) {
177177
std::unique_ptr<CompilationDatabase> Database(
178178
JSONCompilationDatabase::loadFromBuffer(JSONDatabase, ErrorMessage,
179179
JSONCommandLineSyntax::Gnu));
180180
if (!Database)
181181
return CompileCommand();
182+
// Overwrite the string to verify we're not reading from it later.
183+
JSONDatabase.assign(JSONDatabase.size(), '*');
182184
std::vector<CompileCommand> Commands = Database->getCompileCommands(FileName);
183185
EXPECT_LE(Commands.size(), 1u);
184186
if (Commands.empty())

0 commit comments

Comments
 (0)