1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef TOOLS_CLANG_PLUGINS_HESIENDIAGNOSTICBUILDER_H_
6 #define TOOLS_CLANG_PLUGINS_HESIENDIAGNOSTICBUILDER_H_
8 #include "clang/Basic/Diagnostic.h"
9 #include "clang/Basic/SourceLocation.h"
11 namespace chrome_checker
{
13 // A simple wrapper around DiagnosticBuilder that allows a diagnostic to be
14 // suppressed. The intended use case is for helper functions that return a
15 // DiagnosticBuilder, but only want to emit the diagnostic if some conditions
17 class SuppressibleDiagnosticBuilder
: public clang::DiagnosticBuilder
{
19 SuppressibleDiagnosticBuilder(clang::DiagnosticsEngine
* diagnostics
,
20 clang::SourceLocation loc
,
21 unsigned diagnostic_id
,
23 : DiagnosticBuilder(diagnostics
->Report(loc
, diagnostic_id
)),
24 diagnostics_(diagnostics
),
25 suppressed_(suppressed
) {}
27 ~SuppressibleDiagnosticBuilder() {
29 // Clear the counts and underlying data, so the base class destructor
30 // doesn't try to emit the diagnostic.
33 // Also clear the current diagnostic being processed by the
34 // DiagnosticsEngine, since it won't be emitted.
35 diagnostics_
->Clear();
40 friend const SuppressibleDiagnosticBuilder
& operator<<(
41 const SuppressibleDiagnosticBuilder
& builder
,
43 const DiagnosticBuilder
& base_builder
= builder
;
44 base_builder
<< value
;
49 clang::DiagnosticsEngine
* const diagnostics_
;
50 const bool suppressed_
;
53 } // namespace chrome_checker
55 #endif // TOOLS_CLANG_PLUGINS_HESIENDIAGNOSTICBUILDER_H_