Skip to content

Commit ea6c9ca

Browse files
committed
[clang][Sema] Add diagnostic note for function-like macros requiring parentheses
1 parent 7b348f9 commit ea6c9ca

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5961,6 +5961,8 @@ def err_fold_expression_limit_exceeded: Error<
59615961
"instantiating fold expression with %0 arguments exceeded expression nesting "
59625962
"limit of %1">, DefaultFatal, NoSFINAE;
59635963

5964+
def note_function_like_macro_requires_parens : Note<
5965+
"'%0' exists, but as a function-like macro; perhaps, did you forget the parentheses?">;
59645966
def err_unexpected_typedef : Error<
59655967
"unexpected type name %0: expected expression">;
59665968
def err_unexpected_namespace : Error<

clang/lib/Sema/SemaExpr.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,19 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R,
25222522
DC = DC->getLookupParent();
25232523
}
25242524

2525+
// Check whether a similar function-like macro exists and suggest it
2526+
if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
2527+
if (II->hasMacroDefinition()) {
2528+
MacroInfo *MI = PP.getMacroInfo(II);
2529+
if (MI && MI->isFunctionLike()) {
2530+
Diag( R.getNameLoc() ,diag::err_undeclared_var_use) << II->getName();
2531+
Diag(MI->getDefinitionLoc(), diag::note_function_like_macro_requires_parens)
2532+
<< II->getName();
2533+
return true;
2534+
}
2535+
}
2536+
}
2537+
25252538
// We didn't find anything, so try to correct for a typo.
25262539
TypoCorrection Corrected;
25272540
if (S && Out) {
@@ -2632,7 +2645,7 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R,
26322645
<< SS.getRange();
26332646
return true;
26342647
}
2635-
2648+
26362649
// Give up, we can't recover.
26372650
Diag(R.getNameLoc(), diagnostic) << Name;
26382651
return true;

0 commit comments

Comments
 (0)