https://github.com/StarOne01 updated 
https://github.com/llvm/llvm-project/pull/123495

>From 6f2ce4c05c0e03b1c18c694ddea97dac184e2218 Mon Sep 17 00:00:00 2001
From: Prashanth <thestaron...@proton.me>
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 1/3] [clang][Sema] Add diagnostic note for function-like
 macros requiring parentheses

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td |  2 ++
 clang/lib/Sema/SemaExpr.cpp                      | 15 ++++++++++++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index db54312ad965e8..9d98cd2d8bbc88 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5936,6 +5936,8 @@ def err_fold_expression_limit_exceeded: Error<
   "instantiating fold expression with %0 arguments exceeded expression nesting 
"
   "limit of %1">, DefaultFatal, NoSFINAE;
 
+def note_function_like_macro_requires_parens : Note<
+  "'%0' exists, but as a function-like macro; perhaps, did you forget the 
parentheses?">;
 def err_unexpected_typedef : Error<
   "unexpected type name %0: expected expression">;
 def err_unexpected_namespace : Error<
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index ae40895980d90a..f9dd731d59a2dd 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2509,6 +2509,19 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec 
&SS, LookupResult &R,
     DC = DC->getLookupParent();
   }
 
+  // Check whether a similar function-like macro exists and suggest it
+  if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
+    if (II->hasMacroDefinition()) {
+      MacroInfo *MI = PP.getMacroInfo(II);
+      if (MI && MI->isFunctionLike()) {
+        Diag( R.getNameLoc() ,diag::err_undeclared_var_use) << II->getName();
+        Diag(MI->getDefinitionLoc(), 
diag::note_function_like_macro_requires_parens)
+            << II->getName();
+        return true;
+      }
+    }
+  }
+
   // We didn't find anything, so try to correct for a typo.
   TypoCorrection Corrected;
   if (S && Out) {
@@ -2619,7 +2632,7 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec 
&SS, LookupResult &R,
       << SS.getRange();
     return true;
   }
-
+  
   // Give up, we can't recover.
   Diag(R.getNameLoc(), diagnostic) << Name;
   return true;

>From 8e90d9a0a227442d1220f1d6f521f81b6397e146 Mon Sep 17 00:00:00 2001
From: Prashanth <thestaron...@proton.me>
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 2/3] [clang][Tests] Modify tests for function-like macros
 according to the new behavior and Format the changes

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td        | 5 +++--
 clang/lib/Sema/SemaExpr.cpp                             | 7 ++++---
 clang/test/Preprocessor/macro_with_initializer_list.cpp | 6 ++++--
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9d98cd2d8bbc88..37a9a67ac1efba 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5936,8 +5936,9 @@ def err_fold_expression_limit_exceeded: Error<
   "instantiating fold expression with %0 arguments exceeded expression nesting 
"
   "limit of %1">, DefaultFatal, NoSFINAE;
 
-def note_function_like_macro_requires_parens : Note<
-  "'%0' exists, but as a function-like macro; perhaps, did you forget the 
parentheses?">;
+def note_function_like_macro_requires_parens
+    : Note<"'%0' exists, but as a function-like macro; perhaps, did you forget 
"
+           "the parentheses?">;
 def err_unexpected_typedef : Error<
   "unexpected type name %0: expected expression">;
 def err_unexpected_namespace : Error<
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index f9dd731d59a2dd..44bab3e47233cb 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2514,8 +2514,9 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec 
&SS, LookupResult &R,
     if (II->hasMacroDefinition()) {
       MacroInfo *MI = PP.getMacroInfo(II);
       if (MI && MI->isFunctionLike()) {
-        Diag( R.getNameLoc() ,diag::err_undeclared_var_use) << II->getName();
-        Diag(MI->getDefinitionLoc(), 
diag::note_function_like_macro_requires_parens)
+        Diag(R.getNameLoc(), diag::err_undeclared_var_use) << II->getName();
+        Diag(MI->getDefinitionLoc(),
+             diag::note_function_like_macro_requires_parens)
             << II->getName();
         return true;
       }
@@ -2632,7 +2633,7 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec 
&SS, LookupResult &R,
       << SS.getRange();
     return true;
   }
-  
+
   // Give up, we can't recover.
   Diag(R.getNameLoc(), diagnostic) << Name;
   return true;
diff --git a/clang/test/Preprocessor/macro_with_initializer_list.cpp 
b/clang/test/Preprocessor/macro_with_initializer_list.cpp
index 40f53164b263d9..cc7dae0b5a3e00 100644
--- a/clang/test/Preprocessor/macro_with_initializer_list.cpp
+++ b/clang/test/Preprocessor/macro_with_initializer_list.cpp
@@ -134,6 +134,7 @@ void test_NE() {
 // CHECK: fix-it:"{{.*}}macro_with_initializer_list.cpp":{110:32-110:32}:")"
 
 #define INIT(var, init) Foo var = init; // expected-note 3{{defined here}}
+// expected-note@-1 2{{'INIT' exists, but as a function-like macro; perhaps, 
did you forget the parentheses?}}
 // Can't use an initializer list as a macro argument.  The commas in the list
 // will be interpretted as argument separaters and adding parenthesis will
 // make it no longer an initializer list.
@@ -159,12 +160,13 @@ void test() {
   // expected-note@-3 {{cannot use initializer list at the beginning of a 
macro argument}}
 }
 
-// CHECK: fix-it:"{{.*}}macro_with_initializer_list.cpp":{145:11-145:11}:"("
-// CHECK: fix-it:"{{.*}}macro_with_initializer_list.cpp":{145:23-145:23}:")"
+// CHECK: fix-it:"{{.*}}macro_with_initializer_list.cpp":{146:11-146:11}:"("
+// CHECK: fix-it:"{{.*}}macro_with_initializer_list.cpp":{146:23-146:23}:")"
 
 #define M(name,a,b,c,d,e,f,g,h,i,j,k,l) \
   Foo name = a + b + c + d + e + f + g + h + i + j + k + l;
 // expected-note@-2 2{{defined here}}
+// expected-note@-3 {{'M' exists, but as a function-like macro; perhaps, did 
you forget the parentheses?}}
 void test2() {
   M(F1, Foo(), Foo(), Foo(), Foo(), Foo(), Foo(),
         Foo(), Foo(), Foo(), Foo(), Foo(), Foo());

>From 230b171ccfad348ed318d5ef8cf42f67acd2ddad Mon Sep 17 00:00:00 2001
From: Prashanth <thestaron...@proton.me>
Date: Mon, 20 Jan 2025 13:34:48 +0530
Subject: [PATCH 3/3] Change the note for reference of function-like macros
 requiring without parentheses

Co-authored-by: Sirraide <aeternalm...@gmail.com>
---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 37a9a67ac1efba..ecabb622080614 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5937,8 +5937,7 @@ def err_fold_expression_limit_exceeded: Error<
   "limit of %1">, DefaultFatal, NoSFINAE;
 
 def note_function_like_macro_requires_parens
-    : Note<"'%0' exists, but as a function-like macro; perhaps, did you forget 
"
-           "the parentheses?">;
+    : Note<"'%0' is defined here as a function-like macro; did you mean to 
write '%0(...)'">;
 def err_unexpected_typedef : Error<
   "unexpected type name %0: expected expression">;
 def err_unexpected_namespace : Error<

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to