SuppressibleDiagnosticBuilder.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #ifndef TOOLS_CLANG_PLUGINS_HESIENDIAGNOSTICBUILDER_H_
  5. #define TOOLS_CLANG_PLUGINS_HESIENDIAGNOSTICBUILDER_H_
  6. #include "clang/Basic/Diagnostic.h"
  7. #include "clang/Basic/SourceLocation.h"
  8. namespace chrome_checker {
  9. // A simple wrapper around DiagnosticBuilder that allows a diagnostic to be
  10. // suppressed. The intended use case is for helper functions that return a
  11. // DiagnosticBuilder, but only want to emit the diagnostic if some conditions
  12. // are met.
  13. class SuppressibleDiagnosticBuilder : public clang::DiagnosticBuilder {
  14. public:
  15. SuppressibleDiagnosticBuilder(clang::DiagnosticsEngine* diagnostics,
  16. clang::SourceLocation loc,
  17. unsigned diagnostic_id,
  18. bool suppressed)
  19. : DiagnosticBuilder(diagnostics->Report(loc, diagnostic_id)),
  20. diagnostics_(diagnostics),
  21. suppressed_(suppressed) {}
  22. ~SuppressibleDiagnosticBuilder() {
  23. if (suppressed_) {
  24. // Clear the counts and underlying data, so the base class destructor
  25. // doesn't try to emit the diagnostic.
  26. FlushCounts();
  27. Clear();
  28. // Also clear the current diagnostic being processed by the
  29. // DiagnosticsEngine, since it won't be emitted.
  30. diagnostics_->Clear();
  31. }
  32. }
  33. template <typename T>
  34. friend const SuppressibleDiagnosticBuilder& operator<<(
  35. const SuppressibleDiagnosticBuilder& builder,
  36. const T& value) {
  37. const DiagnosticBuilder& base_builder = builder;
  38. base_builder << value;
  39. return builder;
  40. }
  41. private:
  42. clang::DiagnosticsEngine* const diagnostics_;
  43. const bool suppressed_;
  44. };
  45. } // namespace chrome_checker
  46. #endif // TOOLS_CLANG_PLUGINS_HESIENDIAGNOSTICBUILDER_H_