r183389 - Improve c-style cast detection.
Daniel Jasper
djasper at google.com
Thu Jun 6 01:20:21 PDT 2013
Author: djasper
Date: Thu Jun 6 03:20:20 2013
New Revision: 183389
URL: http://llvm.org/viewvc/llvm-project?rev=183389&view=rev
Log:
Improve c-style cast detection.
Before:
return (my_int) aaaa;
template <> void f<int>(int i)SOME_ANNOTATION;
f("aaaa" SOME_MACRO(aaaa)"aaaa");
After:
return (my_int)aaaa;
template <> void f<int>(int i) SOME_ANNOTATION;
f("aaaa" SOME_MACRO(aaaa) "aaaa");
Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=183389&r1=183388&r2=183389&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Jun 6 03:20:20 2013
@@ -626,12 +626,15 @@ private:
Contexts.back().IsExpression)
IsCast = true;
if (Current.Next &&
+ Current.Next->isNot(tok::string_literal) &&
(Current.Next->Tok.isLiteral() ||
Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
IsCast = true;
// If there is an identifier after the (), it is likely a cast, unless
// there is also an identifier before the ().
- if (LeftOfParens && LeftOfParens->Tok.getIdentifierInfo() == NULL &&
+ if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL ||
+ LeftOfParens->is(tok::kw_return)) &&
+ LeftOfParens->Type != TT_TemplateCloser &&
LeftOfParens->Type != TT_ObjCMethodExpr && Current.Next &&
(Current.Next->is(tok::identifier)))
IsCast = true;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=183389&r1=183388&r2=183389&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Jun 6 03:20:20 2013
@@ -3246,6 +3246,7 @@ TEST_F(FormatTest, FormatsCasts) {
verifyFormat("int a = (int)+2;");
verifyFormat("my_int a = (my_int)2.0f;");
verifyFormat("my_int a = (my_int)sizeof(int);");
+ verifyFormat("return (my_int)aaa;");
// FIXME: Without type knowledge, this can still fall apart miserably.
verifyFormat("void f() { my_int a = (my_int) * b; }");
@@ -3271,6 +3272,8 @@ TEST_F(FormatTest, FormatsCasts) {
verifyFormat("void f(int i = (kA * kB) & kMask) {}");
verifyFormat("int a = sizeof(int) * b;");
verifyFormat("int a = alignof(int) * b;");
+ verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
+ verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
// These are not casts, but at some point were confused with casts.
verifyFormat("virtual void foo(int *) override;");
More information about the cfe-commits
mailing list