[clang-tools-extra] r268555 - [clang-tidy] Apply NOLINT filtering to Clang warnings.

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Wed May 4 14:18:31 PDT 2016


Author: alexfh
Date: Wed May  4 16:18:31 2016
New Revision: 268555

URL: http://llvm.org/viewvc/llvm-project?rev=268555&view=rev
Log:
[clang-tidy] Apply NOLINT filtering to Clang warnings.

Modified:
    clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
    clang-tools-extra/trunk/test/clang-tidy/nolint.cpp

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp?rev=268555&r1=268554&r2=268555&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp Wed May  4 16:18:31 2016
@@ -174,20 +174,6 @@ DiagnosticBuilder ClangTidyContext::diag
     StringRef CheckName, SourceLocation Loc, StringRef Description,
     DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
   assert(Loc.isValid());
-  bool Invalid;
-  const char *CharacterData =
-      DiagEngine->getSourceManager().getCharacterData(Loc, &Invalid);
-  if (!Invalid) {
-    const char *P = CharacterData;
-    while (*P != '\0' && *P != '\r' && *P != '\n')
-      ++P;
-    StringRef RestOfLine(CharacterData, P - CharacterData + 1);
-    // FIXME: Handle /\bNOLINT\b(\([^)]*\))?/ as cpplint.py does.
-    if (RestOfLine.find("NOLINT") != StringRef::npos) {
-      Level = DiagnosticIDs::Ignored;
-      ++Stats.ErrorsIgnoredNOLINT;
-    }
-  }
   unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(
       Level, (Description + " [" + CheckName + "]").str());
   if (CheckNamesByDiagnosticID.count(ID) == 0)
@@ -290,8 +276,31 @@ void ClangTidyDiagnosticConsumer::finali
   LastErrorPassesLineFilter = false;
 }
 
+static bool LineIsMarkedWithNOLINT(SourceManager& SM, SourceLocation Loc) {
+  bool Invalid;
+  const char *CharacterData = SM.getCharacterData(Loc, &Invalid);
+  if (!Invalid) {
+    const char *P = CharacterData;
+    while (*P != '\0' && *P != '\r' && *P != '\n')
+      ++P;
+    StringRef RestOfLine(CharacterData, P - CharacterData + 1);
+    // FIXME: Handle /\bNOLINT\b(\([^)]*\))?/ as cpplint.py does.
+    if (RestOfLine.find("NOLINT") != StringRef::npos) {
+      return true;
+    }
+  }
+  return false;
+}
+
 void ClangTidyDiagnosticConsumer::HandleDiagnostic(
     DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
+  if (Info.getLocation().isValid() &&
+      DiagLevel != DiagnosticsEngine::Error &&
+      DiagLevel != DiagnosticsEngine::Fatal &&
+      LineIsMarkedWithNOLINT(Diags->getSourceManager(), Info.getLocation())) {
+    ++Context.Stats.ErrorsIgnoredNOLINT;
+    return;
+  }
   // Count warnings/errors.
   DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
 

Modified: clang-tools-extra/trunk/test/clang-tidy/nolint.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/nolint.cpp?rev=268555&r1=268554&r2=268555&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/nolint.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/nolint.cpp Wed May  4 16:18:31 2016
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s google-explicit-constructor %t
+// RUN: %check_clang_tidy %s google-explicit-constructor,clang-diagnostic-unused-variable %t -- -extra-arg=-Wunused-variable --
 
 class A { A(int i); };
 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: single-argument constructors must be marked explicit
@@ -6,4 +6,11 @@ class A { A(int i); };
 class B { B(int i); }; // NOLINT
 
 class C { C(int i); }; // NOLINT(we-dont-care-about-categories-yet)
-// CHECK-MESSAGES: Suppressed 2 warnings (2 NOLINT)
+
+void f() {
+  int i;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: unused variable 'i' [clang-diagnostic-unused-variable]
+  int j; // NOLINT
+}
+
+// CHECK-MESSAGES: Suppressed 3 warnings (3 NOLINT)




More information about the cfe-commits mailing list