r222193 - Make DiagnosticsEngine::takeClient return std::unique_ptr<>
Alexander Kornienko
alexfh at google.com
Mon Nov 17 15:46:03 PST 2014
Author: alexfh
Date: Mon Nov 17 17:46:02 2014
New Revision: 222193
URL: http://llvm.org/viewvc/llvm-project?rev=222193&view=rev
Log:
Make DiagnosticsEngine::takeClient return std::unique_ptr<>
Summary:
Make DiagnosticsEngine::takeClient return std::unique_ptr<>. Updated
callers to store conditional ownership using a pair of pointer and unique_ptr
instead of a pointer + bool. Updated code that temporarily registers clients to
use the non-owning registration (+ removed extra calls to takeClient).
Reviewers: dblaikie
Reviewed By: dblaikie
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D6294
Modified:
cfe/trunk/include/clang/Basic/Diagnostic.h
cfe/trunk/include/clang/Frontend/VerifyDiagnosticConsumer.h
cfe/trunk/include/clang/Rewrite/Frontend/FixItRewriter.h
cfe/trunk/lib/Basic/Diagnostic.cpp
cfe/trunk/lib/Frontend/ASTUnit.cpp
cfe/trunk/lib/Frontend/CompilerInstance.cpp
cfe/trunk/lib/Frontend/Rewrite/FixItRewriter.cpp
cfe/trunk/lib/Frontend/VerifyDiagnosticConsumer.cpp
cfe/trunk/tools/driver/driver.cpp
cfe/trunk/unittests/Sema/ExternalSemaSourceTest.cpp
Modified: cfe/trunk/include/clang/Basic/Diagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Diagnostic.h?rev=222193&r1=222192&r2=222193&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Diagnostic.h (original)
+++ cfe/trunk/include/clang/Basic/Diagnostic.h Mon Nov 17 17:46:02 2014
@@ -187,7 +187,7 @@ private:
IntrusiveRefCntPtr<DiagnosticIDs> Diags;
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
DiagnosticConsumer *Client;
- bool OwnsDiagClient;
+ std::unique_ptr<DiagnosticConsumer> Owner;
SourceManager *SourceMgr;
/// \brief Mapping information for diagnostics.
@@ -347,7 +347,6 @@ public:
DiagnosticOptions *DiagOpts,
DiagnosticConsumer *client = nullptr,
bool ShouldOwnClient = true);
- ~DiagnosticsEngine();
const IntrusiveRefCntPtr<DiagnosticIDs> &getDiagnosticIDs() const {
return Diags;
@@ -368,14 +367,11 @@ public:
const DiagnosticConsumer *getClient() const { return Client; }
/// \brief Determine whether this \c DiagnosticsEngine object own its client.
- bool ownsClient() const { return OwnsDiagClient; }
-
+ bool ownsClient() const { return Owner != nullptr; }
+
/// \brief Return the current diagnostic client along with ownership of that
/// client.
- DiagnosticConsumer *takeClient() {
- OwnsDiagClient = false;
- return Client;
- }
+ std::unique_ptr<DiagnosticConsumer> takeClient() { return std::move(Owner); }
bool hasSourceManager() const { return SourceMgr != nullptr; }
SourceManager &getSourceManager() const {
Modified: cfe/trunk/include/clang/Frontend/VerifyDiagnosticConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/VerifyDiagnosticConsumer.h?rev=222193&r1=222192&r2=222193&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/VerifyDiagnosticConsumer.h (original)
+++ cfe/trunk/include/clang/Frontend/VerifyDiagnosticConsumer.h Mon Nov 17 17:46:02 2014
@@ -212,7 +212,7 @@ public:
private:
DiagnosticsEngine &Diags;
DiagnosticConsumer *PrimaryClient;
- bool OwnsPrimaryClient;
+ std::unique_ptr<DiagnosticConsumer> PrimaryClientOwner;
std::unique_ptr<TextDiagnosticBuffer> Buffer;
const Preprocessor *CurrentPreprocessor;
const LangOptions *LangOpts;
Modified: cfe/trunk/include/clang/Rewrite/Frontend/FixItRewriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/Frontend/FixItRewriter.h?rev=222193&r1=222192&r2=222193&view=diff
==============================================================================
--- cfe/trunk/include/clang/Rewrite/Frontend/FixItRewriter.h (original)
+++ cfe/trunk/include/clang/Rewrite/Frontend/FixItRewriter.h Mon Nov 17 17:46:02 2014
@@ -66,7 +66,7 @@ class FixItRewriter : public DiagnosticC
/// \brief The diagnostic client that performs the actual formatting
/// of error messages.
DiagnosticConsumer *Client;
- bool OwnsClient;
+ std::unique_ptr<DiagnosticConsumer> Owner;
/// \brief Turn an input path into an output path. NULL implies overwriting
/// the original.
Modified: cfe/trunk/lib/Basic/Diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Diagnostic.cpp?rev=222193&r1=222192&r2=222193&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/Diagnostic.cpp (original)
+++ cfe/trunk/lib/Basic/Diagnostic.cpp Mon Nov 17 17:46:02 2014
@@ -33,13 +33,11 @@ static void DummyArgToStringFn(Diagnosti
Output.append(Str.begin(), Str.end());
}
-
DiagnosticsEngine::DiagnosticsEngine(
- const IntrusiveRefCntPtr<DiagnosticIDs> &diags,
- DiagnosticOptions *DiagOpts,
- DiagnosticConsumer *client, bool ShouldOwnClient)
- : Diags(diags), DiagOpts(DiagOpts), Client(client),
- OwnsDiagClient(ShouldOwnClient), SourceMgr(nullptr) {
+ const IntrusiveRefCntPtr<DiagnosticIDs> &diags, DiagnosticOptions *DiagOpts,
+ DiagnosticConsumer *client, bool ShouldOwnClient)
+ : Diags(diags), DiagOpts(DiagOpts), Client(nullptr), SourceMgr(nullptr) {
+ setClient(client, ShouldOwnClient);
ArgToStringFn = DummyArgToStringFn;
ArgToStringCookie = nullptr;
@@ -63,18 +61,10 @@ DiagnosticsEngine::DiagnosticsEngine(
Reset();
}
-DiagnosticsEngine::~DiagnosticsEngine() {
- if (OwnsDiagClient)
- delete Client;
-}
-
void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
bool ShouldOwnClient) {
- if (OwnsDiagClient && Client)
- delete Client;
-
+ Owner.reset(ShouldOwnClient ? client : nullptr);
Client = client;
- OwnsDiagClient = ShouldOwnClient;
}
void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=222193&r1=222192&r2=222193&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Mon Nov 17 17:46:02 2014
@@ -589,6 +589,7 @@ class CaptureDroppedDiagnostics {
DiagnosticsEngine &Diags;
StoredDiagnosticConsumer Client;
DiagnosticConsumer *PreviousClient;
+ std::unique_ptr<DiagnosticConsumer> OwningPreviousClient;
public:
CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags,
@@ -596,16 +597,15 @@ public:
: Diags(Diags), Client(StoredDiags), PreviousClient(nullptr)
{
if (RequestCapture || Diags.getClient() == nullptr) {
- PreviousClient = Diags.takeClient();
- Diags.setClient(&Client);
+ OwningPreviousClient = Diags.takeClient();
+ PreviousClient = Diags.getClient();
+ Diags.setClient(&Client, false);
}
}
~CaptureDroppedDiagnostics() {
- if (Diags.getClient() == &Client) {
- Diags.takeClient();
- Diags.setClient(PreviousClient);
- }
+ if (Diags.getClient() == &Client)
+ Diags.setClient(PreviousClient, !!OwningPreviousClient.release());
}
};
Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=222193&r1=222192&r2=222193&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Mon Nov 17 17:46:02 2014
@@ -159,9 +159,8 @@ static void SetUpDiagnosticLog(Diagnosti
if (CodeGenOpts)
Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
assert(Diags.ownsClient());
- Diags.setClient(new ChainedDiagnosticConsumer(
- std::unique_ptr<DiagnosticConsumer>(Diags.takeClient()),
- std::move(Logger)));
+ Diags.setClient(
+ new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger)));
}
static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts,
@@ -172,8 +171,7 @@ static void SetupSerializedDiagnostics(D
if (Diags.ownsClient()) {
Diags.setClient(new ChainedDiagnosticConsumer(
- std::unique_ptr<DiagnosticConsumer>(Diags.takeClient()),
- std::move(SerializedConsumer)));
+ Diags.takeClient(), std::move(SerializedConsumer)));
} else {
Diags.setClient(new ChainedDiagnosticConsumer(
Diags.getClient(), std::move(SerializedConsumer)));
Modified: cfe/trunk/lib/Frontend/Rewrite/FixItRewriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/Rewrite/FixItRewriter.cpp?rev=222193&r1=222192&r2=222193&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/Rewrite/FixItRewriter.cpp (original)
+++ cfe/trunk/lib/Frontend/Rewrite/FixItRewriter.cpp Mon Nov 17 17:46:02 2014
@@ -36,14 +36,13 @@ FixItRewriter::FixItRewriter(Diagnostics
FixItOpts(FixItOpts),
NumFailures(0),
PrevDiagSilenced(false) {
- OwnsClient = Diags.ownsClient();
- Client = Diags.takeClient();
- Diags.setClient(this);
+ Owner = Diags.takeClient();
+ Client = Diags.getClient();
+ Diags.setClient(this, false);
}
FixItRewriter::~FixItRewriter() {
- Diags.takeClient();
- Diags.setClient(Client, OwnsClient);
+ Diags.setClient(Client, Owner.release() != nullptr);
}
bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
@@ -188,12 +187,10 @@ void FixItRewriter::Diag(SourceLocation
// When producing this diagnostic, we temporarily bypass ourselves,
// clear out any current diagnostic, and let the downstream client
// format the diagnostic.
- Diags.takeClient();
- Diags.setClient(Client);
+ Diags.setClient(Client, false);
Diags.Clear();
Diags.Report(Loc, DiagID);
- Diags.takeClient();
- Diags.setClient(this);
+ Diags.setClient(this, false);
}
FixItOptions::~FixItOptions() {}
Modified: cfe/trunk/lib/Frontend/VerifyDiagnosticConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/VerifyDiagnosticConsumer.cpp?rev=222193&r1=222192&r2=222193&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/VerifyDiagnosticConsumer.cpp (original)
+++ cfe/trunk/lib/Frontend/VerifyDiagnosticConsumer.cpp Mon Nov 17 17:46:02 2014
@@ -29,12 +29,11 @@ typedef VerifyDiagnosticConsumer::Expect
VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &Diags_)
: Diags(Diags_),
- PrimaryClient(Diags.getClient()), OwnsPrimaryClient(Diags.ownsClient()),
+ PrimaryClient(Diags.getClient()), PrimaryClientOwner(Diags.takeClient()),
Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(nullptr),
LangOpts(nullptr), SrcManager(nullptr), ActiveSourceFiles(0),
Status(HasNoDirectives)
{
- Diags.takeClient();
if (Diags.hasSourceManager())
setSourceManager(Diags.getSourceManager());
}
@@ -43,10 +42,8 @@ VerifyDiagnosticConsumer::~VerifyDiagnos
assert(!ActiveSourceFiles && "Incomplete parsing of source files!");
assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!");
SrcManager = nullptr;
- CheckDiagnostics();
- Diags.takeClient();
- if (OwnsPrimaryClient)
- delete PrimaryClient;
+ CheckDiagnostics();
+ Diags.takeClient().release();
}
#ifndef NDEBUG
@@ -802,8 +799,8 @@ void VerifyDiagnosticConsumer::UpdatePar
void VerifyDiagnosticConsumer::CheckDiagnostics() {
// Ensure any diagnostics go to the primary client.
- bool OwnsCurClient = Diags.ownsClient();
- DiagnosticConsumer *CurClient = Diags.takeClient();
+ DiagnosticConsumer *CurClient = Diags.getClient();
+ std::unique_ptr<DiagnosticConsumer> Owner = Diags.takeClient();
Diags.setClient(PrimaryClient, false);
#ifndef NDEBUG
@@ -865,8 +862,7 @@ void VerifyDiagnosticConsumer::CheckDiag
Buffer->note_end(), "note"));
}
- Diags.takeClient();
- Diags.setClient(CurClient, OwnsCurClient);
+ Diags.setClient(CurClient, Owner.release() != nullptr);
// Reset the buffer, we have processed all the diagnostics in it.
Buffer.reset(new TextDiagnosticBuffer());
Modified: cfe/trunk/tools/driver/driver.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/driver/driver.cpp?rev=222193&r1=222192&r2=222193&view=diff
==============================================================================
--- cfe/trunk/tools/driver/driver.cpp (original)
+++ cfe/trunk/tools/driver/driver.cpp Mon Nov 17 17:46:02 2014
@@ -451,8 +451,7 @@ int main(int argc_, const char **argv_)
clang::serialized_diags::create(DiagOpts->DiagnosticSerializationFile,
&*DiagOpts, /*MergeChildRecords=*/true);
Diags.setClient(new ChainedDiagnosticConsumer(
- std::unique_ptr<DiagnosticConsumer>(Diags.takeClient()),
- std::move(SerializedConsumer)));
+ Diags.takeClient(), std::move(SerializedConsumer)));
}
ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false);
Modified: cfe/trunk/unittests/Sema/ExternalSemaSourceTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Sema/ExternalSemaSourceTest.cpp?rev=222193&r1=222192&r2=222193&view=diff
==============================================================================
--- cfe/trunk/unittests/Sema/ExternalSemaSourceTest.cpp (original)
+++ cfe/trunk/unittests/Sema/ExternalSemaSourceTest.cpp Mon Nov 17 17:46:02 2014
@@ -154,7 +154,7 @@ protected:
DiagnosticsEngine &Diagnostics = CI.getDiagnostics();
DiagnosticConsumer *Client = Diagnostics.getClient();
if (Diagnostics.ownsClient())
- OwnedClient.reset(Diagnostics.takeClient());
+ OwnedClient = Diagnostics.takeClient();
for (size_t I = 0, E = Watchers.size(); I < E; ++I)
Client = Watchers[I]->Chain(Client);
Diagnostics.setClient(Client, false);
More information about the cfe-commits
mailing list