From 2b56649cd2ea2d28cb40a110529a2ffb1a21e9d3 Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Tue, 26 May 2015 10:47:48 +0000 Subject: [PATCH] [clang-tidy] Don't issue most google-readability-casting warnings on .c files included in other files. This is done sometimes for testing purposes, and the check needs to handle this consistently. llvm-svn: 238193 --- .../clang-tidy/google/AvoidCStyleCastsCheck.cpp | 19 +++++++++++-------- .../test/clang-tidy/google-readability-casting.c | 11 +++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp index cc9bce7..485f926 100644 --- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -97,15 +97,19 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { // compiled as C++. if (getCurrentMainFile().endswith(".c")) return; - + // Ignore code in .c files #included in other files (which shouldn't be done, + // but people still do this for test and other purposes). + SourceManager &SM = *Result.SourceManager; + if (SM.getFilename(SM.getSpellingLoc(CastExpr->getLocStart())).endswith(".c")) + return; // Leave type spelling exactly as it was (unlike // getTypeAsWritten().getAsString() which would spell enum types 'enum X'). - StringRef DestTypeString = Lexer::getSourceText( - CharSourceRange::getTokenRange( - CastExpr->getLParenLoc().getLocWithOffset(1), - CastExpr->getRParenLoc().getLocWithOffset(-1)), - *Result.SourceManager, Result.Context->getLangOpts()); + StringRef DestTypeString = + Lexer::getSourceText(CharSourceRange::getTokenRange( + CastExpr->getLParenLoc().getLocWithOffset(1), + CastExpr->getRParenLoc().getLocWithOffset(-1)), + SM, Result.Context->getLangOpts()); auto diag_builder = diag(CastExpr->getLocStart(), "C-style casts are discouraged. %0"); @@ -118,8 +122,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) { if (!isa(SubExpr)) { CastText.push_back('('); diag_builder << FixItHint::CreateInsertion( - Lexer::getLocForEndOfToken(SubExpr->getLocEnd(), 0, - *Result.SourceManager, + Lexer::getLocForEndOfToken(SubExpr->getLocEnd(), 0, SM, Result.Context->getLangOpts()), ")"); } 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 ac5a7c1..8237576 100644 --- a/clang-tools-extra/test/clang-tidy/google-readability-casting.c +++ b/clang-tools-extra/test/clang-tidy/google-readability-casting.c @@ -2,11 +2,22 @@ // 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}}:' +// RUN: cp %s %t.main_file.cpp +// RUN: clang-tidy --checks=-*,google-readability-casting -header-filter='.*' %t.main_file.cpp -- -I%S -DTEST_INCLUDE -x c++ | FileCheck %s -check-prefix=CHECK-MESSAGES -implicit-check-not='{{warning|error}}:' // REQUIRES: shell +#ifdef TEST_INCLUDE + +#undef TEST_INCLUDE +#include "google-readability-casting.c" + +#else + void f(const char *cpc) { const char *cpc2 = (const char*)cpc; // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type [google-readability-casting] // CHECK-FIXES: const char *cpc2 = cpc; char *pc = (char*)cpc; } + +#endif -- 2.7.4