aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@mac.com>2009-12-09 03:30:09 +0000
committerAnders Carlsson <andersca@mac.com>2009-12-09 03:30:09 +0000
commit9f89dd783d9abb9539bd7e952e5823301415c076 (patch)
tree17cf37d0a99f49083a720fbd61f59dc7226b7ab6
parent386ca78d0b03b1fb519e60d1a14cd12a220364a6 (diff)
Move the missing prototypes checking out into a new function. Don't warn about inline functions. Add a test.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@90938 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Sema/SemaDecl.cpp55
-rw-r--r--test/SemaCXX/warn-missing-prototypes.cpp19
2 files changed, 57 insertions, 17 deletions
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 472dc94bc5..54e6f88fd8 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -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
index 0000000000..481547036e
--- /dev/null
+++ b/test/SemaCXX/warn-missing-prototypes.cpp
@@ -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