r215215 - CompilationDatabase: Sure-up ownership of compilation databases using std::unique_ptr
David Blaikie
dblaikie at gmail.com
Fri Aug 8 09:06:15 PDT 2014
Author: dblaikie
Date: Fri Aug 8 11:06:15 2014
New Revision: 215215
URL: http://llvm.org/viewvc/llvm-project?rev=215215&view=rev
Log:
CompilationDatabase: Sure-up ownership of compilation databases using std::unique_ptr
Diving into the memory leaks fixed by r213851 there was one case of a
memory leak of a CompilationDatabase due to not properly taking
ownership of the result of "CompilationDatabase::autoDetectFromSource".
Given that both implementations and callers have been using unique_ptr
to own CompilationDatabase objects - make this explicit in the API to
reduce the risk of further leaks.
Modified:
cfe/trunk/include/clang/Tooling/CompilationDatabase.h
cfe/trunk/include/clang/Tooling/JSONCompilationDatabase.h
cfe/trunk/lib/Tooling/CommonOptionsParser.cpp
cfe/trunk/lib/Tooling/CompilationDatabase.cpp
cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp
cfe/trunk/tools/libclang/CXCompilationDatabase.cpp
Modified: cfe/trunk/include/clang/Tooling/CompilationDatabase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/CompilationDatabase.h?rev=215215&r1=215214&r2=215215&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/CompilationDatabase.h (original)
+++ cfe/trunk/include/clang/Tooling/CompilationDatabase.h Fri Aug 8 11:06:15 2014
@@ -84,22 +84,22 @@ public:
/// FIXME: Currently only supports JSON compilation databases, which
/// are named 'compile_commands.json' in the given directory. Extend this
/// for other build types (like ninja build files).
- static CompilationDatabase *loadFromDirectory(StringRef BuildDirectory,
- std::string &ErrorMessage);
+ static std::unique_ptr<CompilationDatabase>
+ loadFromDirectory(StringRef BuildDirectory, std::string &ErrorMessage);
/// \brief Tries to detect a compilation database location and load it.
///
/// Looks for a compilation database in all parent paths of file 'SourceFile'
/// by calling loadFromDirectory.
- static CompilationDatabase *autoDetectFromSource(StringRef SourceFile,
- std::string &ErrorMessage);
+ static std::unique_ptr<CompilationDatabase>
+ autoDetectFromSource(StringRef SourceFile, std::string &ErrorMessage);
/// \brief Tries to detect a compilation database location and load it.
///
/// Looks for a compilation database in directory 'SourceDir' and all
/// its parent paths by calling loadFromDirectory.
- static CompilationDatabase *autoDetectFromDirectory(StringRef SourceDir,
- std::string &ErrorMessage);
+ static std::unique_ptr<CompilationDatabase>
+ autoDetectFromDirectory(StringRef SourceDir, std::string &ErrorMessage);
/// \brief Returns all compile commands in which the specified file was
/// compiled.
@@ -142,8 +142,8 @@ public:
/// \brief Loads a compilation database from a build directory.
///
/// \see CompilationDatabase::loadFromDirectory().
- virtual CompilationDatabase *loadFromDirectory(StringRef Directory,
- std::string &ErrorMessage) = 0;
+ virtual std::unique_ptr<CompilationDatabase>
+ loadFromDirectory(StringRef Directory, std::string &ErrorMessage) = 0;
};
/// \brief A compilation database that returns a single compile command line.
Modified: cfe/trunk/include/clang/Tooling/JSONCompilationDatabase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/JSONCompilationDatabase.h?rev=215215&r1=215214&r2=215215&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/JSONCompilationDatabase.h (original)
+++ cfe/trunk/include/clang/Tooling/JSONCompilationDatabase.h Fri Aug 8 11:06:15 2014
@@ -53,14 +53,14 @@ public:
///
/// Returns NULL and sets ErrorMessage if the database could not be
/// loaded from the given file.
- static JSONCompilationDatabase *loadFromFile(StringRef FilePath,
- std::string &ErrorMessage);
+ static std::unique_ptr<JSONCompilationDatabase>
+ loadFromFile(StringRef FilePath, std::string &ErrorMessage);
/// \brief Loads a JSON compilation database from a data buffer.
///
/// Returns NULL and sets ErrorMessage if the database could not be loaded.
- static JSONCompilationDatabase *loadFromBuffer(StringRef DatabaseString,
- std::string &ErrorMessage);
+ static std::unique_ptr<JSONCompilationDatabase>
+ loadFromBuffer(StringRef DatabaseString, std::string &ErrorMessage);
/// \brief Returns all compile comamnds in which the specified file was
/// compiled.
Modified: cfe/trunk/lib/Tooling/CommonOptionsParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/CommonOptionsParser.cpp?rev=215215&r1=215214&r2=215215&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/CommonOptionsParser.cpp (original)
+++ cfe/trunk/lib/Tooling/CommonOptionsParser.cpp Fri Aug 8 11:06:15 2014
@@ -82,11 +82,11 @@ CommonOptionsParser::CommonOptionsParser
if (!Compilations) {
std::string ErrorMessage;
if (!BuildPath.empty()) {
- Compilations.reset(CompilationDatabase::autoDetectFromDirectory(
- BuildPath, ErrorMessage));
+ Compilations =
+ CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage);
} else {
- Compilations.reset(CompilationDatabase::autoDetectFromSource(
- SourcePaths[0], ErrorMessage));
+ Compilations = CompilationDatabase::autoDetectFromSource(SourcePaths[0],
+ ErrorMessage);
}
if (!Compilations)
llvm::report_fatal_error(ErrorMessage);
Modified: cfe/trunk/lib/Tooling/CompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/CompilationDatabase.cpp?rev=215215&r1=215214&r2=215215&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/CompilationDatabase.cpp (original)
+++ cfe/trunk/lib/Tooling/CompilationDatabase.cpp Fri Aug 8 11:06:15 2014
@@ -35,7 +35,7 @@ namespace tooling {
CompilationDatabase::~CompilationDatabase() {}
-CompilationDatabase *
+std::unique_ptr<CompilationDatabase>
CompilationDatabase::loadFromDirectory(StringRef BuildDirectory,
std::string &ErrorMessage) {
std::stringstream ErrorStream;
@@ -45,17 +45,16 @@ CompilationDatabase::loadFromDirectory(S
It != Ie; ++It) {
std::string DatabaseErrorMessage;
std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
- if (CompilationDatabase *DB =
- Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
+ if (std::unique_ptr<CompilationDatabase> DB =
+ Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
return DB;
- else
- ErrorStream << It->getName() << ": " << DatabaseErrorMessage << "\n";
+ ErrorStream << It->getName() << ": " << DatabaseErrorMessage << "\n";
}
ErrorMessage = ErrorStream.str();
return nullptr;
}
-static CompilationDatabase *
+static std::unique_ptr<CompilationDatabase>
findCompilationDatabaseFromDirectory(StringRef Directory,
std::string &ErrorMessage) {
std::stringstream ErrorStream;
@@ -63,8 +62,8 @@ findCompilationDatabaseFromDirectory(Str
while (!Directory.empty()) {
std::string LoadErrorMessage;
- if (CompilationDatabase *DB =
- CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage))
+ if (std::unique_ptr<CompilationDatabase> DB =
+ CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage))
return DB;
if (!HasErrorMessage) {
@@ -79,14 +78,14 @@ findCompilationDatabaseFromDirectory(Str
return nullptr;
}
-CompilationDatabase *
+std::unique_ptr<CompilationDatabase>
CompilationDatabase::autoDetectFromSource(StringRef SourceFile,
std::string &ErrorMessage) {
SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile));
StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
- CompilationDatabase *DB = findCompilationDatabaseFromDirectory(Directory,
- ErrorMessage);
+ std::unique_ptr<CompilationDatabase> DB =
+ findCompilationDatabaseFromDirectory(Directory, ErrorMessage);
if (!DB)
ErrorMessage = ("Could not auto-detect compilation database for file \"" +
@@ -94,13 +93,13 @@ CompilationDatabase::autoDetectFromSourc
return DB;
}
-CompilationDatabase *
+std::unique_ptr<CompilationDatabase>
CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir,
std::string &ErrorMessage) {
SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir));
- CompilationDatabase *DB = findCompilationDatabaseFromDirectory(AbsolutePath,
- ErrorMessage);
+ std::unique_ptr<CompilationDatabase> DB =
+ findCompilationDatabaseFromDirectory(AbsolutePath, ErrorMessage);
if (!DB)
ErrorMessage = ("Could not auto-detect compilation database from directory \"" +
Modified: cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp?rev=215215&r1=215214&r2=215215&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp (original)
+++ cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp Fri Aug 8 11:06:15 2014
@@ -118,15 +118,15 @@ std::vector<std::string> unescapeCommand
}
class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin {
- CompilationDatabase *loadFromDirectory(StringRef Directory,
- std::string &ErrorMessage) override {
+ std::unique_ptr<CompilationDatabase>
+ loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override {
SmallString<1024> JSONDatabasePath(Directory);
llvm::sys::path::append(JSONDatabasePath, "compile_commands.json");
std::unique_ptr<CompilationDatabase> Database(
JSONCompilationDatabase::loadFromFile(JSONDatabasePath, ErrorMessage));
if (!Database)
return nullptr;
- return Database.release();
+ return Database;
}
};
@@ -141,7 +141,7 @@ X("json-compilation-database", "Reads JS
// and thus register the JSONCompilationDatabasePlugin.
volatile int JSONAnchorSource = 0;
-JSONCompilationDatabase *
+std::unique_ptr<JSONCompilationDatabase>
JSONCompilationDatabase::loadFromFile(StringRef FilePath,
std::string &ErrorMessage) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> DatabaseBuffer =
@@ -154,10 +154,10 @@ JSONCompilationDatabase::loadFromFile(St
new JSONCompilationDatabase(DatabaseBuffer->release()));
if (!Database->parse(ErrorMessage))
return nullptr;
- return Database.release();
+ return Database;
}
-JSONCompilationDatabase *
+std::unique_ptr<JSONCompilationDatabase>
JSONCompilationDatabase::loadFromBuffer(StringRef DatabaseString,
std::string &ErrorMessage) {
std::unique_ptr<llvm::MemoryBuffer> DatabaseBuffer(
@@ -166,7 +166,7 @@ JSONCompilationDatabase::loadFromBuffer(
new JSONCompilationDatabase(DatabaseBuffer.release()));
if (!Database->parse(ErrorMessage))
return nullptr;
- return Database.release();
+ return Database;
}
std::vector<CompileCommand>
Modified: cfe/trunk/tools/libclang/CXCompilationDatabase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXCompilationDatabase.cpp?rev=215215&r1=215214&r2=215215&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXCompilationDatabase.cpp (original)
+++ cfe/trunk/tools/libclang/CXCompilationDatabase.cpp Fri Aug 8 11:06:15 2014
@@ -16,8 +16,8 @@ clang_CompilationDatabase_fromDirectory(
std::string ErrorMsg;
CXCompilationDatabase_Error Err = CXCompilationDatabase_NoError;
- CompilationDatabase *db = CompilationDatabase::loadFromDirectory(BuildDir,
- ErrorMsg);
+ std::unique_ptr<CompilationDatabase> db =
+ CompilationDatabase::loadFromDirectory(BuildDir, ErrorMsg);
if (!db) {
fprintf(stderr, "LIBCLANG TOOLING ERROR: %s\n", ErrorMsg.c_str());
@@ -27,7 +27,7 @@ clang_CompilationDatabase_fromDirectory(
if (ErrorCode)
*ErrorCode = Err;
- return db;
+ return db.release();
}
void
More information about the cfe-commits
mailing list