From: Sebastian Redl Date: Sun, 28 Dec 2008 15:28:59 +0000 (+0000) Subject: Diagnose declarations that don't declare anything, and fix PR3020. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a4ed0d8d75212dc01b4438829a4b0c846d99458d;p=clang Diagnose declarations that don't declare anything, and fix PR3020. Examples: int; typedef int; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61454 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Parse/DeclSpec.h b/include/clang/Parse/DeclSpec.h index 8b2033bfbe..2fd00dd640 100644 --- a/include/clang/Parse/DeclSpec.h +++ b/include/clang/Parse/DeclSpec.h @@ -318,6 +318,10 @@ public: /// things like "_Imaginary" (lacking an FP type). After calling this method, /// DeclSpec is guaranteed self-consistent, even if an error occurred. void Finish(Diagnostic &D, SourceManager& SrcMgr, const LangOptions &Lang); + + /// isMissingDeclaratorOk - This checks if this DeclSpec can stand alone, + /// without a Declarator. Only tag declspecs can stand alone. + bool isMissingDeclaratorOk(); }; /// ObjCDeclSpec - This class captures information about diff --git a/lib/Parse/DeclSpec.cpp b/lib/Parse/DeclSpec.cpp index b0ac36870c..258dc87c44 100644 --- a/lib/Parse/DeclSpec.cpp +++ b/lib/Parse/DeclSpec.cpp @@ -313,3 +313,11 @@ void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr, // 'data definition has no type or storage class'? } +bool DeclSpec::isMissingDeclaratorOk() { + TST tst = getTypeSpecType(); + return (tst == TST_union + || tst == TST_struct + || tst == TST_class + || tst == TST_enum + ) && getTypeRep() != 0; +} diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 3225628a9d..94e8a3cccc 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -302,7 +302,7 @@ public: /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with /// no declarator (e.g. "struct foo;") is parsed. - virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS); + virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS); virtual DeclTy *ActOnTag(Scope *S, unsigned TagType, TagKind TK, SourceLocation KWLoc, const CXXScopeSpec &SS, diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 1f6cbc4f32..875f0f19ad 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -773,10 +773,17 @@ bool Sema::CheckParmsForFunctionDef(FunctionDecl *FD) { /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with /// no declarator (e.g. "struct foo;") is parsed. Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { - // TODO: emit error on 'int;' or 'const enum foo;'. - // TODO: emit error on 'typedef int;' - // if (!DS.isMissingDeclaratorOk()) Diag(...); - + // FIXME: Isn't that more of a parser diagnostic than a sema diagnostic? + if (!DS.isMissingDeclaratorOk()) { + // FIXME: This diagnostic is emitted even when various previous + // errors occurred (see e.g. test/Sema/decl-invalid.c). However, + // DeclSpec has no means of communicating this information, and the + // responsible parser functions are quite far apart. + Diag(DS.getSourceRange().getBegin(), diag::err_no_declarators) + << DS.getSourceRange(); + return 0; + } + return dyn_cast_or_null(static_cast(DS.getTypeRep())); } diff --git a/test/Parser/cxx-template-decl.cpp b/test/Parser/cxx-template-decl.cpp index 53813776e7..79b04cfc79 100644 --- a/test/Parser/cxx-template-decl.cpp +++ b/test/Parser/cxx-template-decl.cpp @@ -5,10 +5,12 @@ export class foo { }; // expected-error {{expected template}} template x; // expected-error {{expected '<' after 'template'}} export template x; // expected-error {{expected '<' after 'template'}} \ // expected-note {{exported templates are unsupported}} -template < ; // expected-error {{parse error}} -template