[clang] 5824d2b - Fix the declaration printer to properly handle prototypes in C
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 17 10:54:16 PST 2022
Author: Aaron Ballman
Date: 2022-02-17T13:54:09-05:00
New Revision: 5824d2bb0f036e631419ae0993fd03d633398266
URL: https://github.com/llvm/llvm-project/commit/5824d2bb0f036e631419ae0993fd03d633398266
DIFF: https://github.com/llvm/llvm-project/commit/5824d2bb0f036e631419ae0993fd03d633398266.diff
LOG: Fix the declaration printer to properly handle prototypes in C
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.
Added:
Modified:
clang/lib/AST/DeclPrinter.cpp
clang/test/Analysis/cfg.c
clang/test/Analysis/designated-initializer.c
clang/test/Analysis/std-c-library-functions-vs-stream-checker.c
clang/test/Analysis/std-c-library-functions.c
clang/test/OpenMP/declare_mapper_ast_print.c
clang/test/OpenMP/declare_reduction_ast_print.c
clang/test/OpenMP/declare_variant_ast_print.c
clang/test/OpenMP/metadirective_ast_print.c
clang/test/PCH/chain-decls.c
clang/test/PCH/chain-macro.c
clang/test/Sema/attr-print.c
clang/test/SemaObjC/static-ivar-ref-1.m
Removed:
################################################################################
diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp
index c3f1d1544f79a..faafe307f03cf 100644
--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -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) {
diff --git a/clang/test/Analysis/cfg.c b/clang/test/Analysis/cfg.c
index 4bd84e689f251..fc2523859e49b 100644
--- a/clang/test/Analysis/cfg.c
+++ b/clang/test/Analysis/cfg.c
@@ -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]
diff --git a/clang/test/Analysis/designated-initializer.c b/clang/test/Analysis/designated-initializer.c
index aba037a3f49bb..6274ed12911a4 100644
--- a/clang/test/Analysis/designated-initializer.c
+++ b/clang/test/Analysis/designated-initializer.c
@@ -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))
diff --git a/clang/test/Analysis/std-c-library-functions-vs-stream-checker.c b/clang/test/Analysis/std-c-library-functions-vs-stream-checker.c
index 61106f1f8d6bc..e895b54158de3 100644
--- a/clang/test/Analysis/std-c-library-functions-vs-stream-checker.c
+++ b/clang/test/Analysis/std-c-library-functions-vs-stream-checker.c
@@ -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)
diff --git a/clang/test/Analysis/std-c-library-functions.c b/clang/test/Analysis/std-c-library-functions.c
index 50b36dc84e68b..a4032298734d0 100644
--- a/clang/test/Analysis/std-c-library-functions.c
+++ b/clang/test/Analysis/std-c-library-functions.c
@@ -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)
diff --git a/clang/test/OpenMP/declare_mapper_ast_print.c b/clang/test/OpenMP/declare_mapper_ast_print.c
index 55ebd8334c587..6b9686f0a15b8 100644
--- a/clang/test/OpenMP/declare_mapper_ast_print.c
+++ b/clang/test/OpenMP/declare_mapper_ast_print.c
@@ -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)
diff --git a/clang/test/OpenMP/declare_reduction_ast_print.c b/clang/test/OpenMP/declare_reduction_ast_print.c
index cdc1685b14ef2..74c28b3219f51 100644
--- a/clang/test/OpenMP/declare_reduction_ast_print.c
+++ b/clang/test/OpenMP/declare_reduction_ast_print.c
@@ -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))
diff --git a/clang/test/OpenMP/declare_variant_ast_print.c b/clang/test/OpenMP/declare_variant_ast_print.c
index d5c2d440f25ae..0df10263cde5e 100644
--- a/clang/test/OpenMP/declare_variant_ast_print.c
+++ b/clang/test/OpenMP/declare_variant_ast_print.c
@@ -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)})
@@ -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);
diff --git a/clang/test/OpenMP/metadirective_ast_print.c b/clang/test/OpenMP/metadirective_ast_print.c
index fbd7e2291330f..6c75cb0592d6c 100644
--- a/clang/test/OpenMP/metadirective_ast_print.c
+++ b/clang/test/OpenMP/metadirective_ast_print.c
@@ -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
diff --git a/clang/test/PCH/chain-decls.c b/clang/test/PCH/chain-decls.c
index 9011f22cdd59c..b745318c091ef 100644
--- a/clang/test/PCH/chain-decls.c
+++ b/clang/test/PCH/chain-decls.c
@@ -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();
diff --git a/clang/test/PCH/chain-macro.c b/clang/test/PCH/chain-macro.c
index b0fd63de46d5a..2aa69661e6827 100644
--- a/clang/test/PCH/chain-macro.c
+++ b/clang/test/PCH/chain-macro.c
@@ -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
diff --git a/clang/test/Sema/attr-print.c b/clang/test/Sema/attr-print.c
index e2364b60dc1d7..cf6b2463c7bbb 100644
--- a/clang/test/Sema/attr-print.c
+++ b/clang/test/Sema/attr-print.c
@@ -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;
diff --git a/clang/test/SemaObjC/static-ivar-ref-1.m b/clang/test/SemaObjC/static-ivar-ref-1.m
index ff48d93508179..cc240091a3f66 100644
--- a/clang/test/SemaObjC/static-ivar-ref-1.m
+++ b/clang/test/SemaObjC/static-ivar-ref-1.m
@@ -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: }
More information about the cfe-commits
mailing list