[PATCH] D91129: Print source location in the error message when parens are missing around sizeof typename and the expression is inside macro expansion

Shivanshu Goyal via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 23:52:46 PST 2020


shivanshu3 created this revision.
shivanshu3 added reviewers: zahen, hans, rsmith.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
shivanshu3 requested review of this revision.

Given the following code:

  void Foo(int);
  
  #define Bar(x) Foo(x)
  
  void Baz()
  {
  	Bar(sizeof int);
  }

The error message which is printed today is this:

  error: expected parentheses around type name in sizeof expression

There is no source location printed whatsoever, so fixing a compile break like this becomes extremely hard in a large codebase.

My change improves the error message. But it doesn't output a FixItHint because I wasn't able to figure out how to get the locations for left and right parens. So any tips would be appreciated.

  <source>:7:2: error: expected parentheses around type name in sizeof expression
          Bar(sizeof int);
          ^


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91129

Files:
  clang/lib/Parse/ParseExpr.cpp
  clang/test/Parser/sizeof-missing-parens.c


Index: clang/test/Parser/sizeof-missing-parens.c
===================================================================
--- /dev/null
+++ clang/test/Parser/sizeof-missing-parens.c
@@ -0,0 +1,11 @@
+// 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}}
+}
Index: clang/lib/Parse/ParseExpr.cpp
===================================================================
--- clang/lib/Parse/ParseExpr.cpp
+++ clang/lib/Parse/ParseExpr.cpp
@@ -2278,12 +2278,20 @@
         Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext);
         ParseDeclarator(DeclaratorInfo);
 
-        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, ")");
+        SourceLocation OpTokLoc = OpTok.getLocation();
+        if (OpTokLoc.isMacroID()) {
+          SourceLocation OpTokExpansionLoc =
+              PP.getSourceManager().getExpansionLoc(OpTokLoc);
+          Diag(OpTokExpansionLoc,
+               diag::err_expected_parentheses_around_typename)
+              << OpTok.getName();
+        } else {
+          SourceLocation LParenLoc = PP.getLocForEndOfToken(OpTokLoc);
+          SourceLocation RParenLoc = PP.getLocForEndOfToken(PrevTokLocation);
+          Diag(LParenLoc, diag::err_expected_parentheses_around_typename)
+              << OpTok.getName() << FixItHint::CreateInsertion(LParenLoc, "(")
+              << FixItHint::CreateInsertion(RParenLoc, ")");
+        }
         isCastExpr = true;
         return ExprEmpty();
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91129.304059.patch
Type: text/x-patch
Size: 1995 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201110/8433696e/attachment.bin>


More information about the cfe-commits mailing list