[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-05-14 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

Oof, sorry for this falling off my radar!

> To demonstrate the uncertain behaviour, let's take a testcase which @shafik 
> suggested.
> 
> When we track the value of *out pointer though the code, we could see that.
> ```
> #define F() 66
> int x = F;  // This 'F' seems make the `*out` non-null
> 
> void g(int);
> 
> void f() {
> F + 1; // but this 'F' seems to have `*out` ptr null
> g(F);  // this one makes  the `*out` ptr non-null 
> }
> ```
> 
> Even though F shouldn't have a typocorrection, given the context.

Unfortunately, I'm not certain why you get that behavior. I think we'd have to 
get into a debugger and really dig around to find out. I would have expected 
all three uses of `F` there to make `*out` ptr non-null. The only distinction I 
can think of is that the odd-man-out is a discarded value expression. But why 
that should matter for typo correction is a mystery to me currently.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-05-14 Thread via cfe-commits

cor3ntin wrote:

@AaronBallman ping

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-27 Thread via cfe-commits

StarOne01 wrote:

@AaronBallman 

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread via cfe-commits

StarOne01 wrote:

> If you're talking about 
> https://github.com/llvm/llvm-project/blob/cb3498c6704daefc6e5221beb757126765737aa7/clang/lib/Sema/SemaExpr.cpp#L2527,
>  I would expect that condition to be true for both code examples because 
> there is a scope (the body of `main`) and there is a `TypoExpr **` passed in 
> from the caller. Am I looking at the right condition? If so, I'm not certain 
> I understand what's unpredictable yet.

Sorry for the confusion, I referred to this conditional statement 
(https://github.com/llvm/llvm-project/blob/main/clang%2Flib%2FSema%2FSemaExpr.cpp#L2525-L2526).

To demonstrate the uncertain behaviour, let's take a testcase which @shafik 
suggested.

When we track the value of `*out` pointer though the code, we could see that.

```c
#define F() 66
int x = F;  // This 'F' seems make the `*out` non-null

void g(int);

void f() {
F + 1; // but this 'F' seems to have `*out` ptr null
g(F);  // this one makes  the `*out` ptr non-null 
}
```

Even though `F` shouldn't have a typocorrection, given the context.


https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

> But actually I was sceptical about a different thing.

Thanks for clarifying!

> > If i understand it right, that particular conditional statement should get 
> > executed for this:
> 
> ```c
> int main() { 
> int var1 = 63;
> int out = var; 
> } 
> ```
> 
> > and should be skipped for:
> 
> ```c
> int main(){
> int var1 = 63;
> int out = notFound;
> }
> ```
> 
> > but the problem is that the conditional statement gets executed for both 
> > the cases, so when i put the new check somewhere below it, the control 
> > never reaches the new check.
> > please correct me if i'm off somewhere :)
> 
> This was my actual concern, but this behaviour seems unpredictable, sometimes 
> happens and sometimes doesn't, but in either cases no identifiers were near 
> to eachother.

If you're talking about 
https://github.com/llvm/llvm-project/blob/cb3498c6704daefc6e5221beb757126765737aa7/clang/lib/Sema/SemaExpr.cpp#L2527,
 I would expect that condition to be true for both code examples because there 
is a scope (the body of `main`) and there is a `TypoExpr **` passed in from the 
caller. Am I looking at the right condition? If so, I'm not certain I 
understand what's unpredictable yet.


https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread via cfe-commits

StarOne01 wrote:

> For example, we don't suggest a correction for this:
> ```
> #define FOO1 12
> int x = FOO;
> ```
> but we do suggest a correction for this:
> ```
> int FOO1 = 12;
> int x = FOO;
> ```

Did thought of that.

> I think it will be challenging to support macros in general because we'd have 
> to be able to handle `#undef` removing macros that were previously defined, 
> etc. I think for right now, it's fine to not have a typo correction.

That's right, i didn't think of `#undef`.

But actually I was sceptical about a different thing.

> If i understand it right, that particular conditional statement should get 
> executed for this:
> ```c int main(){ int var1 = 63; int out = var; }```
> and should be skipped for:
> ```c int main(){ int var1 = 63; int out = notFound; }```
> but the problem is that the conditional statement gets executed for both the 
> cases, so when i put the new check somewhere below it, the control never 
> reaches the new check.
> please correct me if i'm off somewhere :)

This was my actual concern, but this behaviour seems unpredictable, sometimes 
happens and sometimes doesn't, but in all the cases no identifiers were near to 
eachother.





https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

> > Also, i see the typo correction is unpredictable as I addressed above, is 
> > that a expected behaviour?
> 
> @AaronBallman could you help me with this? I do understand the delayed typo 
> thing but how is that being decided? also, I couldn't find that documented.

For this:
```
#define FOO1() 
void f() {
int FOO;
int x = FOO1; // Typo correction to 'FOO' instead of note about 'FOO1()'.
}
```
I believe the issue is because the `CorrectionCandidateCallback` used is 
unaware of macro identifiers (because macros are not declarations and are 
replaced entirely by the time we get to the parser or sema). For example, we 
don't suggest a correction for this:
```
#define FOO1 12
int x = FOO;
```
https://godbolt.org/z/99s8P3vzc
but we do suggest a correction for this:
```
int FOO1 = 12;
int x = FOO;
```
https://godbolt.org/z/v94rjhxY4

I think it will be challenging to support macros in general because we'd have 
to be able to handle `#undef` removing macros that were previously defined, 
etc. I think for right now, it's fine to not have a typo correction.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread Aaron Ballman via cfe-commits


@@ -2347,6 +2347,27 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, 
ExprValueKind VK,
   return E;
 }
 
+// Check whether a similar function-like macro exists and suggest it
+static bool isFunctionLikeMacro(const DeclarationName &Name, Sema &SemaRef,
+const SourceLocation &TypoLoc) {
+
+  if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
+if (II->hasMacroDefinition()) {
+  MacroInfo *MI = SemaRef.PP.getMacroInfo(II);
+  if (MI && MI->isFunctionLike()) {
+SemaRef.Diag(TypoLoc,
+ diag::err_undeclared_var_use_suggest_func_like_macro)
+<< II->getName();

AaronBallman wrote:

Ah, sorry! I didn't pick up that it was reusing `%0`, so reverting back makes 
sense.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread via cfe-commits

StarOne01 wrote:

Done! (Sorry for force pushes 😅, git suddenly decided to act weird).

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread via cfe-commits

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

>From ea6c9ca9cffbf4327fc7bab85e37e2a3c2c0f0cd Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 01/12] [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 bcae9e9f30093..d9c8fcc66acf2 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,6 +5961,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 3cd4010740d19..dff03ac31ef2a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2522,6 +2522,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) {
@@ -2632,7 +2645,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 c274178a0ce3a510eaaa9390b277b860bcc5a2a9 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 02/12] [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 d9c8fcc66acf2..697b92d325240 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,8 +5961,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 dff03ac31ef2a..da894dd3a6d6a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2527,8 +2527,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;
   }
@@ -2645,7 +2646,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_initi

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread via cfe-commits

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

>From ea6c9ca9cffbf4327fc7bab85e37e2a3c2c0f0cd Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 01/11] [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 bcae9e9f30093..d9c8fcc66acf2 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,6 +5961,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 3cd4010740d19..dff03ac31ef2a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2522,6 +2522,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) {
@@ -2632,7 +2645,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 c274178a0ce3a510eaaa9390b277b860bcc5a2a9 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 02/11] [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 d9c8fcc66acf2..697b92d325240 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,8 +5961,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 dff03ac31ef2a..da894dd3a6d6a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2527,8 +2527,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;
   }
@@ -2645,7 +2646,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_initi

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread via cfe-commits


@@ -2347,6 +2347,27 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, 
ExprValueKind VK,
   return E;
 }
 
+// Check whether a similar function-like macro exists and suggest it
+static bool isFunctionLikeMacro(const DeclarationName &Name, Sema &SemaRef,
+const SourceLocation &TypoLoc) {
+
+  if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
+if (II->hasMacroDefinition()) {
+  MacroInfo *MI = SemaRef.PP.getMacroInfo(II);
+  if (MI && MI->isFunctionLike()) {
+SemaRef.Diag(TypoLoc,
+ diag::err_undeclared_var_use_suggest_func_like_macro)
+<< II->getName();

StarOne01 wrote:

Tried it, but gives `'FOO1' is defined as an object-like macro; did you mean 
''FOO1'(...)'?` so reverted back.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread via cfe-commits

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

>From ea6c9ca9cffbf4327fc7bab85e37e2a3c2c0f0cd Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 01/11] [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 bcae9e9f30093..d9c8fcc66acf2 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,6 +5961,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 3cd4010740d19..dff03ac31ef2a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2522,6 +2522,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) {
@@ -2632,7 +2645,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 c274178a0ce3a510eaaa9390b277b860bcc5a2a9 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 02/11] [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 d9c8fcc66acf2..697b92d325240 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,8 +5961,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 dff03ac31ef2a..da894dd3a6d6a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2527,8 +2527,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;
   }
@@ -2645,7 +2646,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_initi

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread via cfe-commits

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

>From ea6c9ca9cffbf4327fc7bab85e37e2a3c2c0f0cd Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 01/12] [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 bcae9e9f30093..d9c8fcc66acf2 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,6 +5961,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 3cd4010740d19..dff03ac31ef2a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2522,6 +2522,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) {
@@ -2632,7 +2645,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 c274178a0ce3a510eaaa9390b277b860bcc5a2a9 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 02/12] [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 d9c8fcc66acf2..697b92d325240 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,8 +5961,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 dff03ac31ef2a..da894dd3a6d6a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2527,8 +2527,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;
   }
@@ -2645,7 +2646,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_initi

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-12 Thread via cfe-commits

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

>From ea6c9ca9cffbf4327fc7bab85e37e2a3c2c0f0cd Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 01/11] [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 bcae9e9f30093..d9c8fcc66acf2 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,6 +5961,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 3cd4010740d19..dff03ac31ef2a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2522,6 +2522,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) {
@@ -2632,7 +2645,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 c274178a0ce3a510eaaa9390b277b860bcc5a2a9 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 02/11] [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 d9c8fcc66acf2..697b92d325240 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,8 +5961,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 dff03ac31ef2a..da894dd3a6d6a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2527,8 +2527,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;
   }
@@ -2645,7 +2646,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_initi

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-11 Thread Aaron Ballman via cfe-commits


@@ -2347,6 +2347,27 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, 
ExprValueKind VK,
   return E;
 }
 
+// Check whether a similar function-like macro exists and suggest it
+static bool isFunctionLikeMacro(const DeclarationName &Name, Sema &SemaRef,
+const SourceLocation &TypoLoc) {

AaronBallman wrote:

It will do it for you, I'm just used to having to point it out. :-)

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-10 Thread via cfe-commits


@@ -2347,6 +2347,27 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, 
ExprValueKind VK,
   return E;
 }
 
+// Check whether a similar function-like macro exists and suggest it
+static bool isFunctionLikeMacro(const DeclarationName &Name, Sema &SemaRef,
+const SourceLocation &TypoLoc) {

StarOne01 wrote:

I guess, clang-fmt should have done this?

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-10 Thread Aaron Ballman via cfe-commits


@@ -2347,6 +2347,27 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, 
ExprValueKind VK,
   return E;
 }
 
+// Check whether a similar function-like macro exists and suggest it
+static bool isFunctionLikeMacro(const DeclarationName &Name, Sema &SemaRef,
+const SourceLocation &TypoLoc) {
+
+  if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
+if (II->hasMacroDefinition()) {
+  MacroInfo *MI = SemaRef.PP.getMacroInfo(II);
+  if (MI && MI->isFunctionLike()) {
+SemaRef.Diag(TypoLoc,
+ diag::err_undeclared_var_use_suggest_func_like_macro)
+<< II->getName();

AaronBallman wrote:

```suggestion
<< II;
```
This will ensure the name is properly quoted by the diagnostics engine.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-10 Thread Aaron Ballman via cfe-commits


@@ -2382,8 +2403,11 @@ static void emitEmptyLookupTypoDiagnostic(
 if (Ctx)
   SemaRef.Diag(TypoLoc, diag::err_no_member) << Typo << Ctx
  << SS.getRange();
-else
+else {
+  if (isFunctionLikeMacro(Typo, SemaRef, TypoLoc))
+return;

AaronBallman wrote:

```suggestion
else if (diagnoseFunctionLikeMacro(SemaRef, Typo, TypoLoc))
  return;
else
```

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-10 Thread Aaron Ballman via cfe-commits


@@ -10855,6 +10857,8 @@ def err_undeclared_use_suggest : Error<
   "use of undeclared %0; did you mean %1?">;
 def err_undeclared_var_use_suggest : Error<
   "use of undeclared identifier %0; did you mean %1?">;
+def err_undeclared_var_use_suggest_func_like_macro
+: Error<"use of undeclared identifier %0; did you mean %0(...)?">;

AaronBallman wrote:

```suggestion
: Error<"%0 is defined as an object-like macro; did you mean '%0(...)'?">;
```

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-10 Thread Aaron Ballman via cfe-commits


@@ -128,6 +128,7 @@ Improvements to Clang's diagnostics
   which are supposed to only exist once per program, but may get duplicated 
when
   built into a shared library.
 - Fixed a bug where Clang's Analysis did not correctly model the destructor 
behavior of ``union`` members (#GH119415).
+- Clang now provides a diagnostic note for ``function-like macros`` that are 
missing the required parentheses.

AaronBallman wrote:

```suggestion
- Clang now provides a diagnostic note for function-like macros that are
  missing the required parentheses (#GH123038).
```

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-10 Thread Aaron Ballman via cfe-commits


@@ -2624,6 +2648,9 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec 
&SS, LookupResult &R,
   }
   R.clear();
 
+  if (isFunctionLikeMacro(Name, SemaRef, R.getNameLoc()))

AaronBallman wrote:

```suggestion
  if (diagnoseFunctionLikeMacro(SemaRef, Name, R.getNameLoc()))
```

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-10 Thread Aaron Ballman via cfe-commits


@@ -2347,6 +2347,27 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, 
ExprValueKind VK,
   return E;
 }
 
+// Check whether a similar function-like macro exists and suggest it
+static bool isFunctionLikeMacro(const DeclarationName &Name, Sema &SemaRef,
+const SourceLocation &TypoLoc) {

AaronBallman wrote:

```suggestion
static bool diagnoseFunctionLikeMacro(Sema &SemaRef, DeclarationName Name, 
SourceLocation TypoLoc) {
```
Needs to be re-wrapped for 80 column limit.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-10 Thread Aaron Ballman via cfe-commits


@@ -2347,6 +2347,27 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, 
ExprValueKind VK,
   return E;
 }
 
+// Check whether a similar function-like macro exists and suggest it

AaronBallman wrote:

```suggestion
// Diagnose when a macro cannot be expanded because it's a function-like macro
// being used as an object-like macro. Returns true if a diagnostic is emitted.
```

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-10 Thread via cfe-commits

StarOne01 wrote:

> Hmm, I don’t know too much about typo correction so I’m probably not the best 
> person to look into that.

Could you tag someone who works on this?

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-10 Thread via cfe-commits

Sirraide wrote:

> Also, i see the typo correction is unpredictable as I addressed above, is 
> that a expected behaviour?

Hmm, I don’t know too much about typo correction so I’m probably not the best 
person to look into that.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-10 Thread via cfe-commits

Sirraide wrote:

> Merging main into your branch tends to do that.

I’m not sure if this is the case, but my theory is that it’s specifically 
creating a new commit on merge that does that. After resolving conflicts, you 
have to run `git merge --continue` instead of committing manually. I always 
merge main instead of rebasing and I’ve never had any issues, but I’ve seen 
this happen to people several times.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread Mark de Wever via cfe-commits

mordante wrote:

I've removed the ones added by your merge.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread via cfe-commits

StarOne01 wrote:

Thanks @mordante, for explaining. but i don't think that i could remove 
reviewer? Could i?

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread Mark de Wever via cfe-commits

mordante wrote:

> Thanks @mordante, for explaining, but i don't think that i could remove a 
> reviewer? Could i?

Do you have commit access for LLVM? If not you might indeed not be able to.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread via cfe-commits

StarOne01 wrote:

> Do you have commit access for LLVM? If not you might indeed not be able to.

No, i don't have commit access.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread via cfe-commits

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

>From ea6c9ca9cffbf4327fc7bab85e37e2a3c2c0f0cd Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 1/9] [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 bcae9e9f3009387..d9c8fcc66acf280 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,6 +5961,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 3cd4010740d1944..dff03ac31ef2aa6 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2522,6 +2522,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) {
@@ -2632,7 +2645,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 c274178a0ce3a510eaaa9390b277b860bcc5a2a9 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 2/9] [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 d9c8fcc66acf280..697b92d32524007 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,8 +5961,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 dff03ac31ef2aa6..da894dd3a6d6a6c 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2527,8 +2527,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;
   }
@@ -2645,7 +2646,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/macr

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread via cfe-commits

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

>From ea6c9ca9cffbf4327fc7bab85e37e2a3c2c0f0cd Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 1/8] [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 bcae9e9f3009387..d9c8fcc66acf280 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,6 +5961,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 3cd4010740d1944..dff03ac31ef2aa6 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2522,6 +2522,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) {
@@ -2632,7 +2645,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 c274178a0ce3a510eaaa9390b277b860bcc5a2a9 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 2/8] [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 d9c8fcc66acf280..697b92d32524007 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5961,8 +5961,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 dff03ac31ef2aa6..da894dd3a6d6a6c 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2527,8 +2527,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;
   }
@@ -2645,7 +2646,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/macr

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread Mark de Wever via cfe-commits

mordante wrote:

> Okay, i have no idea what just happened, why was everyone asked for review? 
> BTW, got everything resolved
> 
> * Add recommended tests.
> 
> * Made the diagnostic message clear and concise.
> 
> * Add release notes
> 
> 
> Also, i see the typo correction is unpredictable as I addressed above, is 
> that a expected behaviour?

Merging main into your branch tends to do that. Please remove the unwanted 
reviewers.
When you rebase your branch this doesn't happen.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread via cfe-commits

StarOne01 wrote:

Okay, i have no idea what happened, why was everyone asked for review? BTW, got 
everything resolved

- Add recommended tests.
- Made the diagnostic message clear and concise.
- Add release notes

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread via cfe-commits

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

>From 6f2ce4c05c0e03b1c18c694ddea97dac184e2218 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 01/17] [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 db54312ad965e89..9d98cd2d8bbc881 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 ae40895980d90a9..f9dd731d59a2dd7 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 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 02/17] [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 9d98cd2d8bbc881..37a9a67ac1efba8 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 f9dd731d59a2dd7..44bab3e47233cb0 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/

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread via cfe-commits

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

>From 6f2ce4c05c0e03b1c18c694ddea97dac184e2218 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 01/16] [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 db54312ad965e89..9d98cd2d8bbc881 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 ae40895980d90a9..f9dd731d59a2dd7 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 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 02/16] [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 9d98cd2d8bbc881..37a9a67ac1efba8 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 f9dd731d59a2dd7..44bab3e47233cb0 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/

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread via cfe-commits

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

>From 6f2ce4c05c0e03b1c18c694ddea97dac184e2218 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 1/8] [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 db54312ad965e89..9d98cd2d8bbc881 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 ae40895980d90a9..f9dd731d59a2dd7 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 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 2/8] [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 9d98cd2d8bbc881..37a9a67ac1efba8 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 f9dd731d59a2dd7..44bab3e47233cb0 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/macr

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-09 Thread via cfe-commits

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

>From 6f2ce4c05c0e03b1c18c694ddea97dac184e2218 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 1/7] [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 db54312ad965e89..9d98cd2d8bbc881 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 ae40895980d90a9..f9dd731d59a2dd7 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 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 2/7] [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 9d98cd2d8bbc881..37a9a67ac1efba8 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 f9dd731d59a2dd7..44bab3e47233cb0 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/macr

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-02-04 Thread via cfe-commits

StarOne01 wrote:

I did find what was wrong

When we try to correct typos, we seem to do it by looking over all the 
identifiers in the Context which includes the **typo itself!**

```cpp
// clang/lib/Sema/SemaLookup.cpp
for (const auto &I : Context.Idents)
Consumer->FoundName(I.getKey());
```
as the typo and the correction (the typo too) are identical, **a correction is 
added**. 

- This always happen if a typo is encountered.

To avoid this, i tried to check for identical identifiers, but in some cases 
identical identifiers are a necessary correction too for example when:  

- a unqualified identifier without the nested namespace.
- a member call without it's obj, etc...

because of this ambiguity, the ` *out` is always defined and non-empty when a 
typo is encountered, which is our problem here.

i'm stuck, any help with fixing it would be appreciated!

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-31 Thread Shafik Yaghmour via cfe-commits


@@ -180,3 +182,11 @@ void test2() {
   // expected-error@-3 {{use of undeclared identifier}}
   // expected-note@-4 {{cannot use initializer list at the beginning of a 
macro argument}}
 }
+
+#define LIM() 10 
+// expected-note@-1 {{'LIM' is defined here as a function-like macro; did you 
mean 'LIM(...)'}}
+
+void test3() {
+  int iter = LIM;

shafik wrote:

We should also cover other cases as well in the tests e.g.:

```cpp
#define F() 42
int x = F;

void g(int);

void f() {
F + 1;
g(F);
}
```

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-31 Thread Shafik Yaghmour via cfe-commits


@@ -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' is defined here as a function-like macro; did you 
mean 'M(...)'}}

shafik wrote:

The addition of this diagnostic here when the diagnostic below is `error: too 
many arguments provided to function-like macro invocation` seems not very 
helpful and too verbose.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-31 Thread Shafik Yaghmour via cfe-commits

https://github.com/shafik commented:

Adding to the current comments.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-31 Thread Shafik Yaghmour via cfe-commits

https://github.com/shafik edited 
https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-22 Thread via cfe-commits

StarOne01 wrote:

Hey @Sirraide , inorder to include the test, we would need to figure out what's 
going on with the `*out` pointer

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-21 Thread via cfe-commits

StarOne01 wrote:

Thanks for the correction @Sirraide , yeah I forgot that area, will add.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-21 Thread via cfe-commits

https://github.com/Sirraide edited 
https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-21 Thread via cfe-commits

https://github.com/Sirraide commented:

The tests are in the wrong directory (you’ve added them to a file in 
`test/Preprocessor`, but this is a Sema change so the test should probably be 
in `test/Sema`).

Additionally, a test involving typo correction would probably be a good idea:
```c
#define FOO1() 
void f() {
int FOO;
int x = FOO1; // Typo correction to 'FOO' instead of note about 'FOO1()'.
}
```

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-21 Thread via cfe-commits

https://github.com/StarOne01 edited 
https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-21 Thread via cfe-commits


@@ -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' is defined here as a function-like macro; did you mean to 
write '%0(...)'">;

StarOne01 wrote:

Done!

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-21 Thread via cfe-commits

StarOne01 wrote:

Is there any other tests we would need to include?

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-21 Thread via cfe-commits

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

>From 6f2ce4c05c0e03b1c18c694ddea97dac184e2218 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 1/6] [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 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 2/6] [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_i

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-21 Thread via cfe-commits

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

>From 6f2ce4c05c0e03b1c18c694ddea97dac184e2218 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 1/5] [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 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 2/5] [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_i

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-21 Thread via cfe-commits

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

>From 6f2ce4c05c0e03b1c18c694ddea97dac184e2218 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 1/4] [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 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 2/4] [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_i

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-20 Thread via cfe-commits


@@ -2509,6 +2509,20 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec 
&SS, LookupResult &R,
 DC = DC->getLookupParent();
   }
 
+  // Check whether a similar function-like macro exists and suggest it

Sirraide wrote:

Hmm, that sounds weird because I don’t think we should be doing any typo 
correction of `notFound` to `var1` here. That said, I’m not too familiar w/ 
typo correction myself.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-20 Thread via cfe-commits

https://github.com/StarOne01 edited 
https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-20 Thread via cfe-commits

https://github.com/StarOne01 edited 
https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-20 Thread via cfe-commits


@@ -2509,6 +2509,20 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec 
&SS, LookupResult &R,
 DC = DC->getLookupParent();
   }
 
+  // Check whether a similar function-like macro exists and suggest it

StarOne01 wrote:

Hey, thanks for asking, let me explain myself.

If i understand it right, [that particular conditional 
statement](https://github.com/llvm/llvm-project/blob/main/clang%2Flib%2FSema%2FSemaExpr.cpp#L2525-L2526)
 should get executed for this:

```c
int main(){
int var1 = 63;
int out = var;
}
```
and should be skipped for:

```c
int main(){
int var1 = 63;
int out = notFound;
}
```

but the problem is that the conditional statement gets executed for **both the 
cases**, so when i put the new check somewhere below it, the control _never_ 
reaches the new check.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-20 Thread Mariya Podchishchaeva via cfe-commits


@@ -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' is defined here as a function-like macro; did you mean to 
write '%0(...)'">;

Fznamznon wrote:

Not sure, but "did you mean" without any additional verb feels a bit more 
aligned with other messages clang can emit
```suggestion
: Note<"'%0' is defined here as a function-like macro; did you mean 
'%0(...)'">;
```

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-20 Thread Mariya Podchishchaeva via cfe-commits


@@ -2509,6 +2509,20 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec 
&SS, LookupResult &R,
 DC = DC->getLookupParent();
   }
 
+  // Check whether a similar function-like macro exists and suggest it

Fznamznon wrote:

> I did try it, but in this case the function returns here itself

I'm not sure I understand what it is the problem with this return? It should 
happen when a typo replacement has been found, which seems not the case for the 
originally reported case, see https://godbolt.org/z/br9n6eGsT .
Could you please elaborate a bit more?

Because that

> Instead of doing this here before typo correction, the note that this pr adds 
> should probably just be emitted right after the error we emit at the very end 
> of this function

Does seem like a correct soluion.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-20 Thread Mariya Podchishchaeva via cfe-commits

https://github.com/Fznamznon edited 
https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-20 Thread Mariya Podchishchaeva via cfe-commits

https://github.com/Fznamznon commented:

Yeah, tests seem to be missing

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-20 Thread via cfe-commits


@@ -2509,6 +2509,20 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec 
&SS, LookupResult &R,
 DC = DC->getLookupParent();
   }
 
+  // Check whether a similar function-like macro exists and suggest it

StarOne01 wrote:

I did try it, but in this case the function returns [here 
itself](https://github.com/llvm/llvm-project/blob/main/clang%2Flib%2FSema%2FSemaExpr.cpp#L2525-L2526)
 which is before giving up, maybe i could put the note inside this conditional?

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-20 Thread via cfe-commits

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

>From 6f2ce4c05c0e03b1c18c694ddea97dac184e2218 Mon Sep 17 00:00:00 2001
From: Prashanth 
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 
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_i

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-19 Thread via cfe-commits

https://github.com/Sirraide commented:

This still needs tests and a release note.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-19 Thread via cfe-commits


@@ -5936,6 +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?">;

Sirraide wrote:

```suggestion
: Note<"'%0' is defined here as a function-like macro; did you mean to 
write '%0(...)'">;
```
Maybe something like that would be bit clearer?

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-19 Thread via cfe-commits


@@ -2509,6 +2509,20 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec 
&SS, LookupResult &R,
 DC = DC->getLookupParent();
   }
 
+  // Check whether a similar function-like macro exists and suggest it

Sirraide wrote:

Instead of doing this here before typo correction, the note that this pr adds 
should probably just be emitted right after the error we emit at the very end 
of this function (or after the one right before that we emit if the SS isn’t 
empty).

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-19 Thread via cfe-commits

https://github.com/Sirraide edited 
https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-19 Thread via cfe-commits


@@ -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?}}

Sirraide wrote:

Er, this note being emitted here is ... not great because we *are* using it (or 
at least trying to use it) as a function-like macro below—to be fair though, 
putting an init list in a function-like macro currently already leads to a 
flood of errors because we can’t recover from it too well, so if this is the 
only case that causes a false positive then it’s not *that* much of an issue 
imo.

https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-18 Thread via cfe-commits

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


This PR enhances the Clang diagnostics to provide better guidance when 
function-like macros are used without parentheses. Additionally, it updates the 
relevant test cases to reflect these changes (#123038 ).

>From 6f2ce4c05c0e03b1c18c694ddea97dac184e2218 Mon Sep 17 00:00:00 2001
From: Prashanth 
Date: Sun, 19 Jan 2025 07:28:24 +0530
Subject: [PATCH 1/2] [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 
Date: Sun, 19 Jan 2025 08:30:39 +0530
Subject: [PATCH 2/2] [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;
 

[clang] [clang][Sema] Add diagnostic note for reference of function-like macros requiring without parentheses (PR #123495)

2025-01-18 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Prashanth (StarOne01)


Changes


This PR enhances the Clang diagnostics to provide better guidance when 
function-like macros are used without parentheses. Additionally, it updates the 
relevant test cases to reflect these changes (#123038 ).

---
Full diff: https://github.com/llvm/llvm-project/pull/123495.diff


3 Files Affected:

- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+3) 
- (modified) clang/lib/Sema/SemaExpr.cpp (+14) 
- (modified) clang/test/Preprocessor/macro_with_initializer_list.cpp (+4-2) 


``diff
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index db54312ad965e8..37a9a67ac1efba 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5936,6 +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 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..44bab3e47233cb 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2509,6 +2509,20 @@ 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) {
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());

``




https://github.com/llvm/llvm-project/pull/123495
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits