Skip to content

Commit

Permalink
[clang][Diagnostics] Provide parameter source range to arity-mismatch…
Browse files Browse the repository at this point in the history
… notes

Consider the following piece of code:
```
void func( int aa,
           int bb,
           int cc) {}

void arity_mismatch() {
  func(2, 4);
}
```
BEFORE:
```
source.cpp:6:3: error: no matching function for call to 'func'
    6 |   func(2, 4);
      |   ^~~~
source.cpp:1:6: note: candidate function not viable: requires 3 arguments, but 2 were provided
    1 | void func( int aa,
      |      ^
```
AFTER:
```
source.cpp:6:3: error: no matching function for call to 'func'
    6 |   func(2, 4);
      |   ^~~~
source.cpp:1:6: note: candidate function not viable: requires 3 arguments, but 2 were provided
    1 | void func( int aa,
      |      ^     ~~~~~~~
    2 |            int bb,
      |            ~~~~~~~
    3 |            int cc) {}
      |            ~~~~~~
```

Reviewed By: cjdb, aaron.ballman

Differential Revision: https://reviews.llvm.org/D153267
  • Loading branch information
hazohelet committed Jun 25, 2023
1 parent b3c8554 commit 409a809
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ Improvements to Clang's diagnostics
- The Fix-It emitted for unused labels used to expand to the next line, which caused
visual oddities now that Clang shows more than one line of code snippet. This has
been fixed and the Fix-It now only spans to the end of the ``:``.
- Clang now underlines the parameter list of function declaration when emitting
a note about the mismatch in the number of arguments.

Bug Fixes in This Version
-------------------------
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6459,7 +6459,8 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn,

// Emit the location of the prototype.
if (!TC && FDecl && !FDecl->getBuiltinID() && !IsExecConfig)
Diag(FDecl->getLocation(), diag::note_callee_decl) << FDecl;
Diag(FDecl->getLocation(), diag::note_callee_decl)
<< FDecl << FDecl->getParametersSourceRange();

return true;
}
Expand Down Expand Up @@ -6504,7 +6505,8 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn,

// Emit the location of the prototype.
if (!TC && FDecl && !FDecl->getBuiltinID() && !IsExecConfig)
Diag(FDecl->getLocation(), diag::note_callee_decl) << FDecl;
Diag(FDecl->getLocation(), diag::note_callee_decl)
<< FDecl << FDecl->getParametersSourceRange();

// This deletes the extra arguments.
Call->shrinkNumArgs(NumParams);
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11037,11 +11037,13 @@ static void DiagnoseArityMismatch(Sema &S, NamedDecl *Found, Decl *D,
if (modeCount == 1 && Fn->getParamDecl(0)->getDeclName())
S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity_one)
<< (unsigned)FnKindPair.first << (unsigned)FnKindPair.second
<< Description << mode << Fn->getParamDecl(0) << NumFormalArgs;
<< Description << mode << Fn->getParamDecl(0) << NumFormalArgs
<< Fn->getParametersSourceRange();
else
S.Diag(Fn->getLocation(), diag::note_ovl_candidate_arity)
<< (unsigned)FnKindPair.first << (unsigned)FnKindPair.second
<< Description << mode << modeCount << NumFormalArgs;
<< Description << mode << modeCount << NumFormalArgs
<< Fn->getParametersSourceRange();

MaybeEmitInheritedConstructorNote(S, Found);
}
Expand Down
11 changes: 11 additions & 0 deletions clang/test/Misc/diag-func-call-ranges.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-print-source-range-info %s 2>&1 | FileCheck %s --strict-whitespace

// CHECK: :{9:3-9:7}: error: too few arguments
// CHECK: :{7:12-7:26}: note: 'func' declared here
// CHECK: :{10:3-10:7}{10:13-10:17}: error: too many arguments
// CHECK: :{7:12-7:26}: note: 'func' declared here
void func( int aa, int bb) {}
void arity_mismatch() {
func(3);
func(3, 4,5, 6);
}
14 changes: 14 additions & 0 deletions clang/test/Misc/diag-func-call-ranges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-print-source-range-info %s 2>&1 | FileCheck %s --strict-whitespace

// CHECK: error: no matching function for call to 'func'

// CHECK: :{[[@LINE+1]]:12-[[@LINE+1]]:18}: note: {{.*}} requires single argument
void func( int aa ) {}
// CHECK: :{[[@LINE+1]]:12-[[@LINE+3]]:18}: note: {{.*}} requires 3 arguments
void func( int aa,
int bb,
int cc) {}

void arity_mismatch() {
func(2, 4);
}

0 comments on commit 409a809

Please sign in to comment.