]> granicus.if.org Git - clang/commitdiff
Move the missing prototypes checking out into a new function. Don't warn about inline...
authorAnders Carlsson <andersca@mac.com>
Wed, 9 Dec 2009 03:30:09 +0000 (03:30 +0000)
committerAnders Carlsson <andersca@mac.com>
Wed, 9 Dec 2009 03:30:09 +0000 (03:30 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@90938 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/SemaCXX/warn-missing-prototypes.cpp [new file with mode: 0644]

index 472dc94bc5a54a940a995ed3dfe277aa19e1ef6a..54e6f88fd834e08b24cddc5960f897ebe1a3f760 100644 (file)
@@ -3984,6 +3984,42 @@ Sema::DeclPtrTy Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope,
   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 @@ Sema::DeclPtrTy Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclPtrTy D) {
   //   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);
diff --git a/test/SemaCXX/warn-missing-prototypes.cpp b/test/SemaCXX/warn-missing-prototypes.cpp
new file mode 100644 (file)
index 0000000..4815470
--- /dev/null
@@ -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