From: Alexander Kornienko Date: Thu, 21 May 2015 14:08:56 +0000 (+0000) Subject: [clang-tidy] Disable google-readability-casting for .c files and their headers. X-Git-Tag: llvmorg-3.7.0-rc1~4090 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=19bbeaf410361a695040f500c862a21c4e745568;p=platform%2Fupstream%2Fllvm.git [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). llvm-svn: 237905 --- diff --git a/clang-tools-extra/clang-tidy/ClangTidy.h b/clang-tools-extra/clang-tidy/ClangTidy.h index 2bd928f..2df17c2 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.h +++ b/clang-tools-extra/clang-tidy/ClangTidy.h @@ -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; diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h index 8248ff1..67ef840 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h @@ -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); diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp index 7d4cf7e..cc9bce7 100644 --- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -93,6 +93,11 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { 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'). diff --git a/clang-tools-extra/test/clang-tidy/google-readability-casting.c b/clang-tools-extra/test/clang-tidy/google-readability-casting.c index 9765365..ac5a7c1 100644 --- a/clang-tools-extra/test/clang-tidy/google-readability-casting.c +++ b/clang-tools-extra/test/clang-tidy/google-readability-casting.c @@ -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) {