[cfe-commits] r90938 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/SemaCXX/warn-missing-prototypes.cpp

Anders Carlsson andersca at mac.com
Tue Dec 8 19:30:10 PST 2009


Author: andersca
Date: Tue Dec  8 21:30:09 2009
New Revision: 90938

URL: http://llvm.org/viewvc/llvm-project?rev=90938&view=rev
Log:
Move the missing prototypes checking out into a new function. Don't warn about inline functions. Add a test.

Added:
    cfe/trunk/test/SemaCXX/warn-missing-prototypes.cpp
Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=90938&r1=90937&r2=90938&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Dec  8 21:30:09 2009
@@ -3984,6 +3984,42 @@
   return ActOnStartOfFunctionDef(FnBodyScope, DP);
 }
 
+static bool ShouldWarnAboutMissingPrototype(const FunctionDecl *FD) {
+  // Don't warn about invalid declarations.
+  if (FD->isInvalidDecl())
+    return false;
+  
+  // Or declarations that aren't global.
+  if (!FD->isGlobal())
+    return false;
+  
+  // Don't warn about C++ member functions.
+  if (isa<CXXMethodDecl>(FD))
+    return false;
+  
+  // Don't warn about 'main'.
+  if (FD->isMain())
+    return false;
+ 
+  // Don't warn about inline functions.
+  if (FD->isInlineSpecified())
+    return false;
+  
+  bool MissingPrototype = true;
+  for (const FunctionDecl *Prev = FD->getPreviousDeclaration();
+       Prev; Prev = Prev->getPreviousDeclaration()) {
+    // Ignore any declarations that occur in function or method
+    // scope, because they aren't visible from the header.
+    if (Prev->getDeclContext()->isFunctionOrMethod())
+      continue;
+      
+    MissingPrototype = !Prev->getType()->isFunctionProtoType();
+    break;
+  }
+    
+  return MissingPrototype;
+}
+
 Sema::DeclPtrTy Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclPtrTy D) {
   // Clear the last template instantiation error context.
   LastTemplateInstantiationErrorContext = ActiveTemplateInstantiation();
@@ -4029,23 +4065,8 @@
   //   prototype declaration. This warning is issued even if the
   //   definition itself provides a prototype. The aim is to detect
   //   global functions that fail to be declared in header files.
-  if (!FD->isInvalidDecl() && FD->isGlobal() && !isa<CXXMethodDecl>(FD) &&
-      !FD->isMain()) {
-    bool MissingPrototype = true;
-    for (const FunctionDecl *Prev = FD->getPreviousDeclaration();
-         Prev; Prev = Prev->getPreviousDeclaration()) {
-      // Ignore any declarations that occur in function or method
-      // scope, because they aren't visible from the header.
-      if (Prev->getDeclContext()->isFunctionOrMethod())
-        continue;
-
-      MissingPrototype = !Prev->getType()->isFunctionProtoType();
-      break;
-    }
-
-    if (MissingPrototype)
-      Diag(FD->getLocation(), diag::warn_missing_prototype) << FD;
-  }
+  if (ShouldWarnAboutMissingPrototype(FD))
+    Diag(FD->getLocation(), diag::warn_missing_prototype) << FD;
 
   if (FnBodyScope)
     PushDeclContext(FnBodyScope, FD);

Added: cfe/trunk/test/SemaCXX/warn-missing-prototypes.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-missing-prototypes.cpp?rev=90938&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/warn-missing-prototypes.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-missing-prototypes.cpp Tue Dec  8 21:30:09 2009
@@ -0,0 +1,19 @@
+// RUN: clang-cc -fsyntax-only -verify -Wmissing-prototypes %s
+
+void f() { } // expected-warning {{no previous prototype for function 'f'}}
+
+namespace NS {
+  void f() { } // expected-warning {{no previous prototype for function 'f'}}
+}
+
+namespace {
+  // Should not warn about anonymous namespaces
+  void f() { }
+}
+
+struct A {
+  // Should not warn about member functions.
+  void f() { }
+};
+
+inline void g() { }
\ No newline at end of file





More information about the cfe-commits mailing list