diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp --- a/clang/lib/Parse/ParseExpr.cpp +++ b/clang/lib/Parse/ParseExpr.cpp @@ -2266,10 +2266,15 @@ SourceLocation LParenLoc = PP.getLocForEndOfToken(OpTok.getLocation()); SourceLocation RParenLoc = PP.getLocForEndOfToken(PrevTokLocation); - Diag(LParenLoc, diag::err_expected_parentheses_around_typename) - << OpTok.getName() - << FixItHint::CreateInsertion(LParenLoc, "(") - << FixItHint::CreateInsertion(RParenLoc, ")"); + if (LParenLoc.isInvalid() || RParenLoc.isInvalid()) { + Diag(OpTok.getLocation(), + diag::err_expected_parentheses_around_typename) + << OpTok.getName(); + } else { + Diag(LParenLoc, diag::err_expected_parentheses_around_typename) + << OpTok.getName() << FixItHint::CreateInsertion(LParenLoc, "(") + << FixItHint::CreateInsertion(RParenLoc, ")"); + } isCastExpr = true; return ExprEmpty(); } diff --git a/clang/test/Parser/sizeof-missing-parens.c b/clang/test/Parser/sizeof-missing-parens.c new file mode 100644 --- /dev/null +++ b/clang/test/Parser/sizeof-missing-parens.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +void Foo(int); + +#define Bar(x) Foo(x) + +void Baz() { + Foo(sizeof int); // expected-error {{expected parentheses around type name in sizeof expression}} + Bar(sizeof int); // expected-error {{expected parentheses around type name in sizeof expression}} +}