<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sat, Aug 16, 2014 at 4:15 AM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div>On Fri, Aug 15, 2014 at 5:53 PM, Alexander Kornienko <<a href="mailto:alexfh@google.com" target="_blank">alexfh@google.com</a>> wrote:<br>


> Author: alexfh<br>
> Date: Fri Aug 15 19:53:20 2014<br>
> New Revision: 215799<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=215799&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=215799&view=rev</a><br>
> Log:<br>
> AvoidCStyleCastsCheck: don't warn on casts in macros<br>
<br>
</div>What's the particular justification?<br></blockquote><div><br></div><div>The main reason is that some libraries provide macros containing casts in their API (e.g. APR_ARRAY_PUSH from APR or MAP_FAILED from mmap.h which is defined as "((void*)(-1))"). We don't want to warn on them and I doubt we can stuff all these libraries with // NOLINTs to avoid warnings in code using them.</div>
<div>
 </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
c-style casts, even hidden in macros, seem like they'd still be a bit<br>
subtle/surprising/etc.<br>
<div><div><br>
><br>
> Modified:<br>
>     clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp<br>
>     clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp<br>
><br>
> Modified: clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp?rev=215799&r1=215798&r2=215799&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp?rev=215799&r1=215798&r2=215799&view=diff</a><br>


> ==============================================================================<br>
> --- clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp (original)<br>
> +++ clang-tools-extra/trunk/clang-tidy/google/AvoidCStyleCastsCheck.cpp Fri Aug 15 19:53:20 2014<br>
> @@ -60,6 +60,12 @@ bool pointedTypesAreEqual(QualType Sourc<br>
>  void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {<br>
>    const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("cast");<br>
><br>
> +  auto ParenRange = CharSourceRange::getTokenRange(CastExpr->getLParenLoc(),<br>
> +                                                   CastExpr->getRParenLoc());<br>
> +  // Ignore casts in macros.<br>
> +  if (ParenRange.getBegin().isMacroID() || ParenRange.getEnd().isMacroID())<br>
> +    return;<br>
> +<br>
>    // Casting to void is an idiomatic way to mute "unused variable" and similar<br>
>    // warnings.<br>
>    if (CastExpr->getTypeAsWritten()->isVoidType())<br>
> @@ -69,8 +75,6 @@ void AvoidCStyleCastsCheck::check(const<br>
>        CastExpr->getSubExprAsWritten()->getType().getCanonicalType();<br>
>    QualType DestType = CastExpr->getTypeAsWritten().getCanonicalType();<br>
><br>
> -  auto ParenRange = CharSourceRange::getTokenRange(CastExpr->getLParenLoc(),<br>
> -                                                   CastExpr->getRParenLoc());<br>
>    if (SourceType == DestType) {<br>
>      diag(CastExpr->getLocStart(), "Redundant cast to the same type.")<br>
>          << FixItHint::CreateRemoval(ParenRange);<br>
> @@ -84,8 +88,6 @@ void AvoidCStyleCastsCheck::check(const<br>
><br>
>    auto ReplaceWithCast = [&](StringRef CastType) {<br>
>      diag_builder << ("Use " + CastType + ".").str();<br>
> -    if (ParenRange.getBegin().isMacroID() || ParenRange.getEnd().isMacroID())<br>
> -      return;<br>
><br>
>      const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();<br>
>      std::string CastText = (CastType + "<" + DestTypeString + ">").str();<br>
><br>
> Modified: clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp?rev=215799&r1=215798&r2=215799&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp?rev=215799&r1=215798&r2=215799&view=diff</a><br>


> ==============================================================================<br>
> --- clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp (original)<br>
> +++ clang-tools-extra/trunk/test/clang-tidy/avoid-c-style-casts.cpp Fri Aug 15 19:53:20 2014<br>
> @@ -105,9 +105,6 @@ void test_templates() {<br>
>  #define CAST(type, value) (type)(value)<br>
>  void macros(double d) {<br>
>    int i = CAST(int, d);<br>
> -  // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast.<br>
> -  // CHECK-FIXES: #define CAST(type, value) (type)(value)<br>
> -  // CHECK-FIXES: int i = CAST(int, d);<br>
>  }<br>
><br>
>  enum E { E1 = 1 };<br>
><br>
><br>
> _______________________________________________<br>
> cfe-commits mailing list<br>
> <a href="mailto:cfe-commits@cs.uiuc.edu" target="_blank">cfe-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</div></div></blockquote></div><br>
</div></div>