Skip to content

Commit

Permalink
Fix the declaration printer to properly handle prototypes in C
Browse files Browse the repository at this point in the history
Previously, we would take a declaration like void f(void) and print it
as void f(). That's correct in C++ as far as it goes, but is incorrect
in C because that converts the function from having a prototype to one
which does not.

This turns out to matter for some of our tests that use the pretty
printer where we'd like to get rid of the K&R prototypes from the test
but can't because the test is checking the pretty printed function
signature, as done with the ARCMT tests.
  • Loading branch information
AaronBallman committed Feb 17, 2022
1 parent 254d6da commit 5824d2b
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 18 deletions.
4 changes: 4 additions & 0 deletions clang/lib/AST/DeclPrinter.cpp
Expand Up @@ -680,6 +680,10 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
if (FT->isVariadic()) {
if (D->getNumParams()) POut << ", ";
POut << "...";
} else if (!D->getNumParams() && !Context.getLangOpts().CPlusPlus) {
// The function has a prototype, so it needs to retain the prototype
// in C.
POut << "void";
}
} else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Analysis/cfg.c
Expand Up @@ -40,7 +40,7 @@ void checkWrap(int i) {
}
}

// CHECK-LABEL: void checkGCCAsmRValueOutput()
// CHECK-LABEL: void checkGCCAsmRValueOutput(void)
// CHECK: [B2 (ENTRY)]
// CHECK-NEXT: Succs (1): B1
// CHECK: [B1]
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Analysis/designated-initializer.c
Expand Up @@ -16,7 +16,7 @@ void test(void) {
};
}

// CHECK: void test()
// CHECK: void test(void)
// CHECK: [B1]
// CHECK: 1: getUQ
// CHECK: 2: [B1.1] (ImplicitCastExpr, FunctionToPointerDecay, union UQ (*)(void))
Expand Down
Expand Up @@ -28,7 +28,7 @@

// Verify that the summaries are loaded when the StdLibraryFunctionsChecker is
// enabled.
// CHECK: Loaded summary for: int getchar()
// CHECK: Loaded summary for: int getchar(void)
// CHECK-NEXT: Loaded summary for: unsigned long fread(void *restrict, size_t, size_t, FILE *restrict)
// CHECK-NEXT: Loaded summary for: unsigned long fwrite(const void *restrict, size_t, size_t, FILE *restrict)

Expand Down
2 changes: 1 addition & 1 deletion clang/test/Analysis/std-c-library-functions.c
Expand Up @@ -52,7 +52,7 @@
// CHECK-NEXT: Loaded summary for: int isxdigit(int)
// CHECK-NEXT: Loaded summary for: int getc(FILE *)
// CHECK-NEXT: Loaded summary for: int fgetc(FILE *)
// CHECK-NEXT: Loaded summary for: int getchar()
// CHECK-NEXT: Loaded summary for: int getchar(void)
// CHECK-NEXT: Loaded summary for: unsigned int fread(void *restrict, size_t, size_t, FILE *restrict)
// CHECK-NEXT: Loaded summary for: unsigned int fwrite(const void *restrict, size_t, size_t, FILE *restrict)
// CHECK-NEXT: Loaded summary for: ssize_t read(int, void *, size_t)
Expand Down
2 changes: 1 addition & 1 deletion clang/test/OpenMP/declare_mapper_ast_print.c
Expand Up @@ -41,7 +41,7 @@ struct dat {
#pragma omp declare mapper(struct dat d) map(to: d.d)
// CHECK: #pragma omp declare mapper (default : struct dat d) map(to: d.d){{$}}

// CHECK: int main() {
// CHECK: int main(void) {
int main(void) {
#pragma omp declare mapper(id: struct vec v) map(v.len)
// CHECK: #pragma omp declare mapper (id : struct vec v) map(tofrom: v.len)
Expand Down
2 changes: 1 addition & 1 deletion clang/test/OpenMP/declare_reduction_ast_print.c
Expand Up @@ -31,7 +31,7 @@ void init(struct SSS *priv, struct SSS orig);
#pragma omp declare reduction(fun : struct SSS : omp_out = omp_in) initializer(init(&omp_priv, omp_orig))
// CHECK: #pragma omp declare reduction (fun : struct SSS : omp_out = omp_in) initializer(init(&omp_priv, omp_orig))

// CHECK: int main() {
// CHECK: int main(void) {
int main(void) {
#pragma omp declare reduction(fun : struct SSS : omp_out = omp_in) initializer(init(&omp_priv, omp_orig))
// CHECK: #pragma omp declare reduction (fun : struct SSS : omp_out = omp_in) initializer(init(&omp_priv, omp_orig))
Expand Down
4 changes: 2 additions & 2 deletions clang/test/OpenMP/declare_variant_ast_print.c
Expand Up @@ -25,7 +25,7 @@ int foo(void);
#pragma omp declare variant(foo) match(implementation={extension(match_none)})
int bar(void);

// CHECK: int foo();
// CHECK: int foo(void);
// CHECK-NEXT: #pragma omp declare variant(foo) match(implementation={extension(match_none)})
// CHECK-NEXT: #pragma omp declare variant(foo) match(implementation={extension(match_any)})
// CHECK-NEXT: #pragma omp declare variant(foo) match(implementation={extension(match_all)})
Expand All @@ -41,4 +41,4 @@ int bar(void);
// CHECK-NEXT: #pragma omp declare variant(foo) match(construct={parallel})
// CHECK-NEXT: #pragma omp declare variant(foo) match(construct={teams})
// CHECK-NEXT: #pragma omp declare variant(foo) match(construct={target})
// CHECK-NEXT: int bar();
// CHECK-NEXT: int bar(void);
4 changes: 2 additions & 2 deletions clang/test/OpenMP/metadirective_ast_print.c
Expand Up @@ -59,8 +59,8 @@ void foo(void) {
}
}

// CHECK: void bar();
// CHECK: void foo()
// CHECK: void bar(void);
// CHECK: void foo(void)
// CHECK-NEXT: #pragma omp parallel
// CHECK-NEXT: bar()
// CHECK-NEXT: #pragma omp parallel
Expand Down
4 changes: 2 additions & 2 deletions clang/test/PCH/chain-decls.c
Expand Up @@ -9,8 +9,8 @@

// expected-no-diagnostics

// CHECK: void f();
// CHECK: void g();
// CHECK: void f(void);
// CHECK: void g(void);

int h(void) {
f();
Expand Down
4 changes: 2 additions & 2 deletions clang/test/PCH/chain-macro.c
Expand Up @@ -4,7 +4,7 @@
// RUN: %clang_cc1 -ast-print -include-pch %t2 %s | FileCheck %s
// expected-no-diagnostics

// CHECK: void f();
// CHECK: void f(void);
FOOBAR
// CHECK: void g();
// CHECK: void g(void);
BARFOO
5 changes: 2 additions & 3 deletions clang/test/Sema/attr-print.c
Expand Up @@ -10,11 +10,10 @@ __declspec(align(4)) int y;
// CHECK: short arr[3] __attribute__((aligned));
short arr[3] __attribute__((aligned));

// FIXME: -ast-print is printing this function signature with a K&R C style.
// CHECK: void foo() __attribute__((const));
// CHECK: void foo(void) __attribute__((const));
void foo(void) __attribute__((const));

// CHECK: void bar() __attribute__((__const));
// CHECK: void bar(void) __attribute__((__const));
void bar(void) __attribute__((__const));

// CHECK: int * __ptr32 p32;
Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaObjC/static-ivar-ref-1.m
Expand Up @@ -24,7 +24,7 @@ int foo(void)
// CHECK: }
// CHECK: @end
// CHECK: current *pc;
// CHECK: int foo() {
// CHECK: int foo(void) {
// CHECK: return pc->ivar2 + (*pc).ivar + pc->ivar1;
// CHECK: }

0 comments on commit 5824d2b

Please sign in to comment.