[llvm-branch-commits] [clang] e53b9f7 - Print source location in the error message when parens are missing around sizeof typename and the expression is inside macro expansion

Richard Smith via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Dec 16 12:18:45 PST 2020


Author: Shivanshu Goyal
Date: 2020-12-16T12:03:31-08:00
New Revision: e53b9f733a7cb0a5da372b73ab6b7711c0300d65

URL: https://github.com/llvm/llvm-project/commit/e53b9f733a7cb0a5da372b73ab6b7711c0300d65
DIFF: https://github.com/llvm/llvm-project/commit/e53b9f733a7cb0a5da372b73ab6b7711c0300d65.diff

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

Given the following code:

```
void Foo(int);

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:6: error: expected parentheses around type name in sizeof expression
        Bar(sizeof int);
            ^
```

Reviewed By: rsmith

Differential Revision: https://reviews.llvm.org/D91129

Added: 
    clang/test/Parser/sizeof-missing-parens.c

Modified: 
    clang/lib/Parse/ParseExpr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp
index d993d9ce4bdb..6acf76d713fd 100644
--- a/clang/lib/Parse/ParseExpr.cpp
+++ b/clang/lib/Parse/ParseExpr.cpp
@@ -2266,10 +2266,15 @@ Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
 
         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
index 000000000000..527f74151be1
--- /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}}
+}


        


More information about the llvm-branch-commits mailing list