From 12d2cc71bfeb1e7be9ce00fc52feab50941cac24 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Wed, 27 Jun 2012 19:43:29 +0000 Subject: [PATCH] patch to suggest 'static' function should be 'static inline' when it appears to be unused and occurs in a header. // rdar://11202617 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@159282 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 4 ++++ lib/Sema/Sema.cpp | 14 +++++++++++--- test/SemaCXX/warn-static-function-inheader.cpp | 12 ++++++++++++ test/SemaCXX/warn-static-function-inheader.h | 3 +++ 4 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 test/SemaCXX/warn-static-function-inheader.cpp create mode 100644 test/SemaCXX/warn-static-function-inheader.h diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 38a22a7fce..fb54050254 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -170,6 +170,10 @@ def warn_used_but_marked_unused: Warning<"%0 was marked unused but was used">, def warn_unneeded_internal_decl : Warning< "%select{function|variable}0 %1 is not needed and will not be emitted">, InGroup, DefaultIgnore; +def warn_unneeded_static_internal_decl : Warning< + "'static' function %0 declared in header file " + "should be declared 'static inline'">, + InGroup, DefaultIgnore; def warn_unneeded_member_function : Warning< "member function %0 is not needed and will not be emitted">, InGroup, DefaultIgnore; diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp index 9d18a5c596..42d5fb7481 100644 --- a/lib/Sema/Sema.cpp +++ b/lib/Sema/Sema.cpp @@ -682,9 +682,17 @@ void Sema::ActOnEndOfTranslationUnit() { if (isa(DiagD)) Diag(DiagD->getLocation(), diag::warn_unneeded_member_function) << DiagD->getDeclName(); - else - Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) - << /*function*/0 << DiagD->getDeclName(); + else { + if (FD->getStorageClassAsWritten() == SC_Static && + !FD->isInlineSpecified() && + !SourceMgr.isFromMainFile( + SourceMgr.getExpansionLoc(FD->getLocation()))) + Diag(DiagD->getLocation(), diag::warn_unneeded_static_internal_decl) + << DiagD->getDeclName(); + else + Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) + << /*function*/0 << DiagD->getDeclName(); + } } else { Diag(DiagD->getLocation(), isa(DiagD) ? diag::warn_unused_member_function diff --git a/test/SemaCXX/warn-static-function-inheader.cpp b/test/SemaCXX/warn-static-function-inheader.cpp new file mode 100644 index 0000000000..30386d9a25 --- /dev/null +++ b/test/SemaCXX/warn-static-function-inheader.cpp @@ -0,0 +1,12 @@ +#include "warn-static-function-inheader.h" +// RUN: %clang_cc1 -fsyntax-only -verify -Wall %s +// rdar://11202617 + +static void another(void) { // expected-warning {{function 'another' is not needed and will not be emitted}} +} + +template +void foo(void) { + thing(); + another(); +} diff --git a/test/SemaCXX/warn-static-function-inheader.h b/test/SemaCXX/warn-static-function-inheader.h new file mode 100644 index 0000000000..1c9e00b8bd --- /dev/null +++ b/test/SemaCXX/warn-static-function-inheader.h @@ -0,0 +1,3 @@ +static void thing(void) { // expected-warning {{'static' function 'thing' declared in header file should be declared 'static inline'}} +} + -- 2.40.0