Skip to content

Commit f9c6e56

Browse files
committed
Show note for -Wmissing-prototypes for functions with parameters
Summary: There was a search for non-prototype declarations for the function, but we only showed the results for zero-parameter functions. Now we show the note for functions with parameters as well, but we omit the fix-it hint suggesting to add `void`. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D62750 llvm-svn: 363748
1 parent 4053d95 commit f9c6e56

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4672,7 +4672,8 @@ def warn_missing_prototype : Warning<
46724672
"no previous prototype for function %0">,
46734673
InGroup<DiagGroup<"missing-prototypes">>, DefaultIgnore;
46744674
def note_declaration_not_a_prototype : Note<
4675-
"this declaration is not a prototype; add 'void' to make it a prototype for a zero-parameter function">;
4675+
"this declaration is not a prototype; add %select{'void'|parameter declarations}0 "
4676+
"to make it %select{a prototype for a zero-parameter function|one}0">;
46764677
def warn_strict_prototypes : Warning<
46774678
"this %select{function declaration is not|block declaration is not|"
46784679
"old-style function definition is not preceded by}0 a prototype">,

clang/lib/Sema/SemaDecl.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12777,8 +12777,9 @@ void Sema::ActOnFinishInlineFunctionDef(FunctionDecl *D) {
1277712777
Consumer.HandleInlineFunctionDefinition(D);
1277812778
}
1277912779

12780-
static bool ShouldWarnAboutMissingPrototype(const FunctionDecl *FD,
12781-
const FunctionDecl*& PossibleZeroParamPrototype) {
12780+
static bool
12781+
ShouldWarnAboutMissingPrototype(const FunctionDecl *FD,
12782+
const FunctionDecl *&PossiblePrototype) {
1278212783
// Don't warn about invalid declarations.
1278312784
if (FD->isInvalidDecl())
1278412785
return false;
@@ -12815,21 +12816,18 @@ static bool ShouldWarnAboutMissingPrototype(const FunctionDecl *FD,
1281512816
if (FD->isDeleted())
1281612817
return false;
1281712818

12818-
bool MissingPrototype = true;
1281912819
for (const FunctionDecl *Prev = FD->getPreviousDecl();
1282012820
Prev; Prev = Prev->getPreviousDecl()) {
1282112821
// Ignore any declarations that occur in function or method
1282212822
// scope, because they aren't visible from the header.
1282312823
if (Prev->getLexicalDeclContext()->isFunctionOrMethod())
1282412824
continue;
1282512825

12826-
MissingPrototype = !Prev->getType()->isFunctionProtoType();
12827-
if (FD->getNumParams() == 0)
12828-
PossibleZeroParamPrototype = Prev;
12829-
break;
12826+
PossiblePrototype = Prev;
12827+
return Prev->getType()->isFunctionNoProtoType();
1283012828
}
1283112829

12832-
return MissingPrototype;
12830+
return true;
1283312831
}
1283412832

1283512833
void
@@ -13349,21 +13347,22 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
1334913347
// prototype declaration. This warning is issued even if the
1335013348
// definition itself provides a prototype. The aim is to detect
1335113349
// global functions that fail to be declared in header files.
13352-
const FunctionDecl *PossibleZeroParamPrototype = nullptr;
13353-
if (ShouldWarnAboutMissingPrototype(FD, PossibleZeroParamPrototype)) {
13350+
const FunctionDecl *PossiblePrototype = nullptr;
13351+
if (ShouldWarnAboutMissingPrototype(FD, PossiblePrototype)) {
1335413352
Diag(FD->getLocation(), diag::warn_missing_prototype) << FD;
1335513353

13356-
if (PossibleZeroParamPrototype) {
13354+
if (PossiblePrototype) {
1335713355
// We found a declaration that is not a prototype,
1335813356
// but that could be a zero-parameter prototype
13359-
if (TypeSourceInfo *TI =
13360-
PossibleZeroParamPrototype->getTypeSourceInfo()) {
13357+
if (TypeSourceInfo *TI = PossiblePrototype->getTypeSourceInfo()) {
1336113358
TypeLoc TL = TI->getTypeLoc();
1336213359
if (FunctionNoProtoTypeLoc FTL = TL.getAs<FunctionNoProtoTypeLoc>())
13363-
Diag(PossibleZeroParamPrototype->getLocation(),
13360+
Diag(PossiblePrototype->getLocation(),
1336413361
diag::note_declaration_not_a_prototype)
13365-
<< PossibleZeroParamPrototype
13366-
<< FixItHint::CreateInsertion(FTL.getRParenLoc(), "void");
13362+
<< (FD->getNumParams() != 0)
13363+
<< (FD->getNumParams() == 0
13364+
? FixItHint::CreateInsertion(FTL.getRParenLoc(), "void")
13365+
: FixItHint{});
1336713366
}
1336813367
}
1336913368

clang/test/Sema/warn-missing-prototypes.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// RUN: %clang_cc1 -fsyntax-only -Wdocumentation -Wmissing-prototypes -verify %s
22
// RUN: %clang_cc1 -fsyntax-only -Wdocumentation -Wmissing-prototypes -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
33

4-
int f();
4+
int f(); // expected-note{{this declaration is not a prototype; add parameter declarations to make it one}}
5+
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:{{.*}}-[[@LINE-1]]:{{.*}}}:"{{.*}}"
56

67
int f(int x) { return x; } // expected-warning{{no previous prototype for function 'f'}}
78

@@ -15,7 +16,8 @@ int g2(int x) { return x; }
1516

1617
void test(void);
1718

18-
int h3();
19+
int h3(); // expected-note{{this declaration is not a prototype; add parameter declarations to make it one}}
20+
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:{{.*}}-[[@LINE-1]]:{{.*}}}:"{{.*}}"
1921
int h4(int);
2022
int h4();
2123

@@ -38,6 +40,5 @@ int f2(int x) { return x; }
3840
int main(void) { return 0; }
3941

4042
void not_a_prototype_test(); // expected-note{{this declaration is not a prototype; add 'void' to make it a prototype for a zero-parameter function}}
43+
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:27-[[@LINE-1]]:27}:"void"
4144
void not_a_prototype_test() { } // expected-warning{{no previous prototype for function 'not_a_prototype_test'}}
42-
43-
// CHECK: fix-it:"{{.*}}":{40:27-40:27}:"void"

0 commit comments

Comments
 (0)