1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "dumper/diagnostic_consumer.h"
16 
17 #include <clang/Basic/DiagnosticCategories.h>
18 #include <clang/Basic/DiagnosticIDs.h>
19 #include <clang/Lex/LexDiagnostic.h>
20 
21 
22 namespace header_checker {
23 namespace dumper {
24 
25 
HeaderCheckerDiagnosticConsumer(std::unique_ptr<clang::DiagnosticConsumer> wrapped)26 HeaderCheckerDiagnosticConsumer::HeaderCheckerDiagnosticConsumer(
27     std::unique_ptr<clang::DiagnosticConsumer> wrapped)
28     : wrapped_(std::move(wrapped)) {}
29 
clear()30 void HeaderCheckerDiagnosticConsumer::clear() {
31   // Default implementation resets warning/error count.
32   DiagnosticConsumer::clear();
33   wrapped_->clear();
34 }
35 
BeginSourceFile(const clang::LangOptions & lang_opts,const clang::Preprocessor * preprocessor)36 void HeaderCheckerDiagnosticConsumer::BeginSourceFile(
37     const clang::LangOptions &lang_opts,
38     const clang::Preprocessor *preprocessor) {
39   wrapped_->BeginSourceFile(lang_opts, preprocessor);
40 }
41 
EndSourceFile()42 void HeaderCheckerDiagnosticConsumer::EndSourceFile() {
43   wrapped_->EndSourceFile();
44 }
45 
finish()46 void HeaderCheckerDiagnosticConsumer::finish() { wrapped_->finish(); }
47 
IncludeInDiagnosticCounts() const48 bool HeaderCheckerDiagnosticConsumer::IncludeInDiagnosticCounts() const {
49   return false;
50 }
51 
HandleDiagnostic(clang::DiagnosticsEngine::Level level,const clang::Diagnostic & info)52 void HeaderCheckerDiagnosticConsumer::HandleDiagnostic(
53     clang::DiagnosticsEngine::Level level, const clang::Diagnostic &info) {
54   if (level < clang::DiagnosticsEngine::Level::Error) {
55     return;
56   }
57   unsigned id = info.getID();
58   if (id == clang::diag::err_pp_hash_error ||
59       id == clang::diag::fatal_too_many_errors) {
60     return;
61   }
62   unsigned category = clang::DiagnosticIDs::getCategoryNumberForDiag(id);
63   if (category == clang::diag::DiagCat_Semantic_Issue) {
64     return;
65   }
66   // Default implementation increases warning/error count.
67   DiagnosticConsumer::HandleDiagnostic(level, info);
68   wrapped_->HandleDiagnostic(level, info);
69 }
70 
71 
72 }  // dumper
73 }  // header_checker
74