summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2015-01-29 15:17:13 +0000
committerAlexander Kornienko <alexfh@google.com>2015-01-29 15:17:13 +0000
commit78070fbae59b29b5fc7b970b948a9991cc9e9bbc (patch)
tree4aea7c22bc6511b6d17d1838cbb37449e04629fe /clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
parent7a2410429378190c32bcc02d499ad592460d7b20 (diff)
downloadbcm5719-llvm-78070fbae59b29b5fc7b970b948a9991cc9e9bbc.tar.gz
bcm5719-llvm-78070fbae59b29b5fc7b970b948a9991cc9e9bbc.zip
[clang-tidy] Fix some false positives in google-readability-casting
Summary: Ignore C-style casts in extern "C" {} sections. Be more careful when detecting redundant casts between typedefs to the same type - emit a more specific warning and don't automatically fix them. Reviewers: klimek Reviewed By: klimek Subscribers: curdeius, cfe-commits Differential Revision: http://reviews.llvm.org/D7247 llvm-svn: 227444
Diffstat (limited to 'clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp24
1 files changed, 18 insertions, 6 deletions
diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
index 093250d541f..20440fe618c 100644
--- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
@@ -68,19 +68,30 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
if (CastExpr->getTypeAsWritten()->isVoidType())
return;
- QualType SourceType =
- CastExpr->getSubExprAsWritten()->getType().getCanonicalType();
- QualType DestType = CastExpr->getTypeAsWritten().getCanonicalType();
+ QualType SourceType = CastExpr->getSubExprAsWritten()->getType();
+ QualType DestType = CastExpr->getTypeAsWritten();
if (SourceType == DestType) {
- diag(CastExpr->getLocStart(), "Redundant cast to the same type.")
+ diag(CastExpr->getLocStart(), "redundant cast to the same type")
<< FixItHint::CreateRemoval(ParenRange);
return;
}
+ SourceType = SourceType.getCanonicalType();
+ DestType = DestType.getCanonicalType();
+ if (SourceType == DestType) {
+ diag(CastExpr->getLocStart(),
+ "possibly redundant cast between typedefs of the same type");
+ return;
+ }
+
// The rest of this check is only relevant to C++.
if (!Result.Context->getLangOpts().CPlusPlus)
return;
+ // Ignore code inside extern "C" {} blocks.
+ if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context)
+ .empty())
+ return;
// Leave type spelling exactly as it was (unlike
// getTypeAsWritten().getAsString() which would spell enum types 'enum X').
@@ -94,7 +105,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
diag(CastExpr->getLocStart(), "C-style casts are discouraged. %0");
auto ReplaceWithCast = [&](StringRef CastType) {
- diag_builder << ("Use " + CastType + ".").str();
+ diag_builder << ("Use " + CastType).str();
const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
std::string CastText = (CastType + "<" + DestTypeString + ">").str();
@@ -108,6 +119,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
}
diag_builder << FixItHint::CreateReplacement(ParenRange, CastText);
};
+
// Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics.
switch (CastExpr->getCastKind()) {
case CK_NoOp:
@@ -145,7 +157,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
break;
}
- diag_builder << "Use static_cast/const_cast/reinterpret_cast.";
+ diag_builder << "Use static_cast/const_cast/reinterpret_cast";
}
} // namespace readability
OpenPOWER on IntegriCloud