• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef TOOLS_CLANG_PLUGINS_HESIENDIAGNOSTICBUILDER_H_
6 #define TOOLS_CLANG_PLUGINS_HESIENDIAGNOSTICBUILDER_H_
7 
8 #include "clang/Basic/Diagnostic.h"
9 #include "clang/Basic/SourceLocation.h"
10 
11 namespace chrome_checker {
12 
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
16 // are met.
17 class SuppressibleDiagnosticBuilder : public clang::DiagnosticBuilder {
18  public:
SuppressibleDiagnosticBuilder(clang::DiagnosticsEngine * diagnostics,clang::SourceLocation loc,unsigned diagnostic_id,bool suppressed)19   SuppressibleDiagnosticBuilder(clang::DiagnosticsEngine* diagnostics,
20                                 clang::SourceLocation loc,
21                                 unsigned diagnostic_id,
22                                 bool suppressed)
23       : DiagnosticBuilder(diagnostics->Report(loc, diagnostic_id)),
24         diagnostics_(diagnostics),
25         suppressed_(suppressed) {}
26 
~SuppressibleDiagnosticBuilder()27   ~SuppressibleDiagnosticBuilder() {
28     if (suppressed_) {
29       // Clear the counts and underlying data, so the base class destructor
30       // doesn't try to emit the diagnostic.
31       FlushCounts();
32       Clear();
33       // Also clear the current diagnostic being processed by the
34       // DiagnosticsEngine, since it won't be emitted.
35       diagnostics_->Clear();
36     }
37   }
38 
39   template <typename T>
40   friend const SuppressibleDiagnosticBuilder& operator<<(
41       const SuppressibleDiagnosticBuilder& builder,
42       const T& value) {
43     const DiagnosticBuilder& base_builder = builder;
44     base_builder << value;
45     return builder;
46   }
47 
48  private:
49   clang::DiagnosticsEngine* const diagnostics_;
50   const bool suppressed_;
51 };
52 
53 }  // namespace chrome_checker
54 
55 #endif  // TOOLS_CLANG_PLUGINS_HESIENDIAGNOSTICBUILDER_H_
56