From: Serge Pavlov Date: Sun, 4 Aug 2019 10:08:51 +0000 (+0000) Subject: [Parser] Emit descriptive diagnostic for misplaced pragma X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0778fc61cf6933981705c2258d59ae82e79c9e24;p=clang [Parser] Emit descriptive diagnostic for misplaced pragma If a class or struct or union declaration contains a pragma that is not valid in this context, compiler issues generic error like "expected member name or ';' after declaration specifiers". With this change the error tells that this pragma cannot appear in this declaration. Differential Revision: https://reviews.llvm.org/D64932 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@367779 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td index 734e7d25d9..453cc01fc5 100644 --- a/include/clang/Basic/DiagnosticParseKinds.td +++ b/include/clang/Basic/DiagnosticParseKinds.td @@ -974,6 +974,8 @@ def warn_pragma_missing_argument : Warning< def warn_pragma_invalid_argument : Warning< "unexpected argument '%0' to '#pragma %1'%select{|; expected %3}2">, InGroup; +def err_pragma_misplaced_in_decl : Error<"this pragma cannot appear in %0 declaration">; + // '#pragma clang section' related errors def err_pragma_expected_clang_section_name : Error< "expected one of [bss|data|rodata|text] section kind in '#pragma %0'">; diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 63a7de454c..5fc36e86b1 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -4148,6 +4148,14 @@ void Parser::ParseStructUnionBody(SourceLocation RecordLoc, continue; } + if (tok::isPragmaAnnotation(Tok.getKind())) { + Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl) + << DeclSpec::getSpecifierName( + TagType, Actions.getASTContext().getPrintingPolicy()); + ConsumeAnnotationToken(); + continue; + } + if (!Tok.is(tok::at)) { auto CFieldCallback = [&](ParsingFieldDeclarator &FD) { // Install the declarator into the current TagDecl. diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index 9126da7a72..8e69bb2295 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -3134,6 +3134,13 @@ Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas( TagDecl); default: + if (tok::isPragmaAnnotation(Tok.getKind())) { + Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl) + << DeclSpec::getSpecifierName(TagType, + Actions.getASTContext().getPrintingPolicy()); + ConsumeAnnotationToken(); + return nullptr; + } return ParseCXXClassMemberDeclaration(AS, AccessAttrs); } } diff --git a/test/Parser/pragma-attribute-context.cpp b/test/Parser/pragma-attribute-context.cpp index d893f18335..bcdd20fe3e 100644 --- a/test/Parser/pragma-attribute-context.cpp +++ b/test/Parser/pragma-attribute-context.cpp @@ -31,8 +31,7 @@ int c = my_ns::nested::h(); // expected-warning{{'h' is only available on macOS struct InStruct { // FIXME: This asserts in Objective-C++! - // FIXME: This is a horrible diagnostic! #ifndef __OBJC__ - BEGIN_PRAGMA // expected-error {{expected member name or ';' after declaration specifiers}} + BEGIN_PRAGMA // expected-error {{this pragma cannot appear in struct declaration}} #endif }; diff --git a/test/Parser/pragma-fp-contract.c b/test/Parser/pragma-fp-contract.c index c80c140ea7..aa25dcdf53 100644 --- a/test/Parser/pragma-fp-contract.c +++ b/test/Parser/pragma-fp-contract.c @@ -10,3 +10,16 @@ void f2(void) { #pragma STDC FP_CONTRACT OFF #pragma STDC FP_CONTRACT ON } + +struct S1 { +// expected-error@+1 {{this pragma cannot appear in struct declaration}} +#pragma STDC FP_CONTRACT ON + float f1; +}; + +union U1 { + float f1; + float f2; +// expected-error@+1 {{this pragma cannot appear in union declaration}} +#pragma STDC FP_CONTRACT ON +}; diff --git a/test/Parser/pragma-fp-contract.cpp b/test/Parser/pragma-fp-contract.cpp new file mode 100644 index 0000000000..c04c6c2db8 --- /dev/null +++ b/test/Parser/pragma-fp-contract.cpp @@ -0,0 +1,32 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +void f1(void) { + int x = 0; +/* expected-error@+1 {{'#pragma fp_contract' can only appear at file scope or at the start of a compound statement}} */ +#pragma STDC FP_CONTRACT ON +} + +void f2(void) { + #pragma STDC FP_CONTRACT OFF + #pragma STDC FP_CONTRACT ON +} + +struct S1 { +// expected-error@+1 {{this pragma cannot appear in struct declaration}} +#pragma STDC FP_CONTRACT ON + float f1; +}; + +union U1 { + float f1; + float f2; +// expected-error@+1 {{this pragma cannot appear in union declaration}} +#pragma STDC FP_CONTRACT ON +}; + +class C1 { + float f1; +// expected-error@+1 {{this pragma cannot appear in class declaration}} +#pragma STDC FP_CONTRACT ON + float f2; +};