From ff1a2e519ebcd6d7a060eac7ba8aca37b2bf89d0 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Thu, 6 Jun 2013 08:20:20 +0000 Subject: [PATCH] Improve c-style cast detection. Before: return (my_int) aaaa; template <> void f(int i)SOME_ANNOTATION; f("aaaa" SOME_MACRO(aaaa)"aaaa"); After: return (my_int)aaaa; template <> void f(int i) SOME_ANNOTATION; f("aaaa" SOME_MACRO(aaaa) "aaaa"); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183389 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 5 ++++- unittests/Format/FormatTest.cpp | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 62177b3efd..fc53681394 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 84e22cbbef..1c2a540409 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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 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;"); -- 2.40.0