[clang-tools-extra] r237905 - [clang-tidy] Disable google-readability-casting for .c files and their headers.

Alexander Kornienko alexfh at google.com
Thu May 21 07:08:56 PDT 2015


Author: alexfh
Date: Thu May 21 09:08:56 2015
New Revision: 237905

URL: http://llvm.org/viewvc/llvm-project?rev=237905&view=rev
Log:
[clang-tidy] Disable google-readability-casting for .c files and their headers.

Some people have reasons to compile their .c files as C++ in some configurations
(e.g. for testing purposes), so just looking at LangOptions is not enough. This
patch disables the check on all .c files (and also for the headers included from
.c files).


Modified:
    clang-tools-extra/trunk/clang-tidy/ClangTidy.h
    clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h
    clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.c

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidy.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidy.h?rev=237905&r1=237904&r2=237905&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.h Thu May 21 09:08:56 2015
@@ -156,6 +156,8 @@ private:
 
 protected:
   OptionsView Options;
+  /// \brief Returns the main file name of the current translation unit.
+  StringRef getCurrentMainFile() const { return Context->getCurrentFile(); }
 };
 
 class ClangTidyCheckFactories;

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h?rev=237905&r1=237904&r2=237905&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h Thu May 21 09:08:56 2015
@@ -143,6 +143,9 @@ public:
   /// \brief Should be called when starting to process new translation unit.
   void setCurrentFile(StringRef File);
 
+  /// \brief Returns the main file name of the current translation unit.
+  StringRef getCurrentFile() const { return CurrentFile; }
+
   /// \brief Sets ASTContext for the current translation unit.
   void setASTContext(ASTContext *Context);
 

Modified: clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp?rev=237905&r1=237904&r2=237905&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp Thu May 21 09:08:56 2015
@@ -93,6 +93,11 @@ void AvoidCStyleCastsCheck::check(const
   if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context)
            .empty())
     return;
+  // Ignore code in .c files and headers included from them, even if they are
+  // compiled as C++.
+  if (getCurrentMainFile().endswith(".c"))
+    return;
+
 
   // Leave type spelling exactly as it was (unlike
   // getTypeAsWritten().getAsString() which would spell enum types 'enum X').

Modified: clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.c
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.c?rev=237905&r1=237904&r2=237905&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.c (original)
+++ clang-tools-extra/trunk/test/clang-tidy/google-readability-casting.c Thu May 21 09:08:56 2015
@@ -1,4 +1,7 @@
 // RUN: $(dirname %s)/check_clang_tidy.sh %s google-readability-casting %t -- -x c
+// The testing script always adds .cpp extension to the input file name, so we
+// need to run clang-tidy directly in order to verify handling of .c files:
+// RUN: clang-tidy --checks=-*,google-readability-casting %s -- -x c++ | FileCheck %s -check-prefix=CHECK-MESSAGES -implicit-check-not='{{warning|error}}:'
 // REQUIRES: shell
 
 void f(const char *cpc) {





More information about the cfe-commits mailing list