diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-01-05 04:12:21 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-01-05 04:12:21 +0000 |
commit | cb7709c06027448c754dd03e2e521d82d04818bf (patch) | |
tree | 4566bb699a7b846fa7bb7893440d6e45278f3e65 /lib/Parse/ParseDecl.cpp | |
parent | ec9ea7200718478e8a976529defbe21942a11c9c (diff) |
PR10828: Produce a warning when a no-arguments function is declared in block
scope, when no other indication is provided that the user intended to declare a
function rather than a variable.
Remove some false positives from the existing 'parentheses disambiguated as a
function' warning by suppressing it when the declaration is marked as 'typedef'
or 'extern'.
Add a new warning group -Wvexing-parse containing both of these warnings.
The new warning is enabled by default; despite a number of false positives (and
one bug) in clang's test-suite, I have only found genuine bugs with it when
running it over a significant quantity of real C++ code.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@147599 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Parse/ParseDecl.cpp')
-rw-r--r-- | lib/Parse/ParseDecl.cpp | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index ee4a51e184..d3c8211372 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -1111,6 +1111,21 @@ Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, if (FirstDecl) DeclsInGroup.push_back(FirstDecl); + // In C++, "T var();" at block scope is probably an attempt to initialize a + // variable, not a function declaration. We don't catch this case earlier, + // since there is no ambiguity here. + if (getLang().CPlusPlus && Context != Declarator::FileContext && + D.getNumTypeObjects() == 1 && D.isFunctionDeclarator() && + D.getDeclSpec().getStorageClassSpecAsWritten() + == DeclSpec::SCS_unspecified && + D.getDeclSpec().getTypeSpecType() != DeclSpec::TST_void) { + DeclaratorChunk &C = D.getTypeObject(0); + if (C.Fun.NumArgs == 0 && !C.Fun.isVariadic && !C.Fun.TrailingReturnType && + C.Fun.getExceptionSpecType() == EST_None) + Diag(C.Loc, diag::warn_empty_parens_are_function_decl) + << SourceRange(C.Loc, C.EndLoc); + } + bool ExpectSemi = Context != Declarator::ForContext; // If we don't have a comma, it is either the end of the list (a ';') or an |