Don't report "main" as missing a prototype in freestanding mode
authorBill Wendling <isanbard@gmail.com>
Fri, 22 Nov 2019 20:35:14 +0000 (12:35 -0800)
committerBill Wendling <isanbard@gmail.com>
Fri, 22 Nov 2019 20:35:43 +0000 (12:35 -0800)
Summary:
A user may want to use freestanding mode with the standard "main" entry
point. It's not useful to warn about a missing prototype as it's not
typical to have a prototype for "main".

Reviewers: efriedma, aaron.ballman

Reviewed By: aaron.ballman

Subscribers: aaron.ballman, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D70588

clang/lib/Sema/SemaDecl.cpp
clang/test/Sema/no-warn-missing-prototype.c [new file with mode: 0644]

index 6d857e8..47c05d5 100644 (file)
@@ -13319,8 +13319,10 @@ ShouldWarnAboutMissingPrototype(const FunctionDecl *FD,
     return false;
 
   // Don't warn about 'main'.
-  if (FD->isMain())
-    return false;
+  if (isa<TranslationUnitDecl>(FD->getDeclContext()->getRedeclContext()))
+    if (IdentifierInfo *II = FD->getIdentifier())
+      if (II->isStr("main"))
+        return false;
 
   // Don't warn about inline functions.
   if (FD->isInlined())
diff --git a/clang/test/Sema/no-warn-missing-prototype.c b/clang/test/Sema/no-warn-missing-prototype.c
new file mode 100644 (file)
index 0000000..2361677
--- /dev/null
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -fsyntax-only -Wmissing-prototypes-x c -ffreestanding -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wmissing-prototypes -x c++ -ffreestanding -verify %s
+// expected-no-diagnostics
+int main() {
+  return 0;
+}