Clang error: expected ';' at end of declaration (err_expected_semi_declaration)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: expected ';' at end of declaration
Type Error
Category Parse Issue
Internal Id err_expected_semi_declaration
Internal Message expected ';' at end of declaration
Regular Expression (?:error|fatal error)\: expected ';' at end of declaration
First Commit 2009-07-31 ef50e9978389 sp.

Description

The error is issued by the Clang compiler when it encounters a declaration statement that is not properly terminated with a semicolon (';'). In C and C++ languages, semicolons are used to mark the end of a declaration or statement. This rule allows the compiler to correctly parse and understand the structure of the code. When a semicolon is omitted at the end of a declaration, the compiler cannot accurately determine where the declaration ends and the next part of the code begins, leading to this error message. The resolution involves adding a semicolon (';') at the point where it is expected, as indicated by the compiler's error message and potentially by a suggested fix-it hint provided by the compiler.  
AI Generated

Example

In the following example, a common error related to the omission of a semicolon in C++ code is illustrated. The code defines a simple main function that attempts to declare an integer variable v and initialize it with the value 1. However, the crucial semicolon that should follow the declaration and initialization of v is missing. This results in a compilation error, as the C++ compiler expects declarations to be terminated with a semicolon. The output clearly shows the compiler pointing out the exact location within the source where the semicolon is expected, helping to direct attention to the cause of the error.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
int main() {
  int v = 1 // Missing ';' here
}
Compiler Output
<source>:2:12: error: expected ';' at end of declaration


Clang Internals (17.0.6)

Git Commit Message

sp.

llvm-svn: 77656

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Parse/ParseDecl.cpp (line 2278)

/// ParseDeclGroup - Having concluded that this is either a function
/// definition or a group of object declarations, actually parse the
/// result.
Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, DeclaratorContext Context, ParsedAttributes &Attrs, SourceLocation *DeclEnd, ForRangeInit *FRI) {
  // ...
  while (TryConsumeToken(tok::comma, CommaLoc)) {
    if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
      // ...
      Diag(CommaLoc, diag::err_expected_semi_declaration) << FixItHint::CreateReplacement(CommaLoc, ";");

clang/lib/Parse/ParseDecl.cpp (line 2326)

/// ParseDeclGroup - Having concluded that this is either a function
/// definition or a group of object declarations, actually parse the
/// result.
Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, DeclaratorContext Context, ParsedAttributes &Attrs, SourceLocation *DeclEnd, ForRangeInit *FRI) {
  // ...
  if (ExpectSemi && ExpectAndConsumeSemi(Context == DeclaratorContext::File ? diag::err_invalid_token_after_toplevel_declarator : diag::err_expected_semi_declaration)) {

clang/lib/Parse/ParseDeclCXX.cpp (line 3166)

/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
///
///       member-declaration:
///         decl-specifier-seq[opt] member-declarator-list[opt] ';'
///         function-definition ';'[opt]
///         ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
///         using-declaration                                            [TODO]
/// [C++0x] static_assert-declaration
///         template-declaration
/// [GNU]   '__extension__' member-declaration
///
///       member-declarator-list:
///         member-declarator
///         member-declarator-list ',' member-declarator
///
///       member-declarator:
///         declarator virt-specifier-seq[opt] pure-specifier[opt]
/// [C++2a] declarator requires-clause
///         declarator constant-initializer[opt]
/// [C++11] declarator brace-or-equal-initializer[opt]
///         identifier[opt] ':' constant-expression
///
///       virt-specifier-seq:
///         virt-specifier
///         virt-specifier-seq virt-specifier
///
///       virt-specifier:
///         override
///         final
/// [MS]    sealed
///
///       pure-specifier:
///         '= 0'
///
///       constant-initializer:
///         '=' constant-expression
///
Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, ParsedAttributes &AccessAttrs, const ParsedTemplateInfo &TemplateInfo, ParsingDeclRAIIObject *TemplateDiags) {
  // ...
  while (true) {
    // ...
    if (Tok.isAtStartOfLine() && !MightBeDeclarator(DeclaratorContext::Member)) {
      // ...
      Diag(CommaLoc, diag::err_expected_semi_declaration) << FixItHint::CreateReplacement(CommaLoc, ";");

clang/lib/Parse/Parser.cpp (line 1597)

/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
/// types for a function with a K&R-style identifier list for arguments.
void Parser::ParseKNRParamDeclarations(Declarator &D) {
  // ...
  // Read all the argument declarations.
  while (isDeclarationSpecifier(ImplicitTypenameContext::No)) {
    // ...
    if (!ExpectAndConsumeSemi(diag::err_expected_semi_declaration))

clang/lib/Parse/ParseStmt.cpp (line 1184)

/// ParseCompoundStatementBody - Parse a sequence of statements optionally
/// followed by a label and invoke the ActOnCompoundStmt action.  This expects
/// the '{' to be the current token, and consume the '}' at the end of the
/// block.  It does not manipulate the scope stack.
StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
  // ...
  // "__label__ X, Y, Z;" is the GNU "Local Label" extension.  These are
  // only allowed at the start of a compound stmt regardless of the language.
  while (Tok.is(tok::kw___label__)) {
    // ...
    ExpectAndConsumeSemi(diag::err_expected_semi_declaration);

clang/lib/Parse/ParseTemplate.cpp (line 373)

/// Parse a single declaration that declares a template,
/// template specialization, or explicit instantiation of a template.
///
/// \param DeclEnd will receive the source location of the last token
/// within this declaration.
///
/// \param AS the access specifier associated with this
/// declaration. Will be AS_none for namespace-scope declarations.
///
/// \returns the new declaration.
Decl *Parser::ParseSingleDeclarationAfterTemplate(DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo, ParsingDeclRAIIObject &DiagsFromTParams, SourceLocation &DeclEnd, ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
  // ...
  ExpectAndConsumeSemi(diag::err_expected_semi_declaration);

clang/lib/Parse/ParseTemplate.cpp (line 454)

/// \brief Parse a single declaration that declares a concept.
///
/// \param DeclEnd will receive the source location of the last token
/// within this declaration.
///
/// \returns the new declaration.
Decl *Parser::ParseConceptDefinition(const ParsedTemplateInfo &TemplateInfo, SourceLocation &DeclEnd) {
  // ...
  ExpectAndConsumeSemi(diag::err_expected_semi_declaration);

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/CXX/lex/lex.literal/lex.ext/p10.cpp

  • clang/test/CXX/lex/lex.literal/lex.ext/p10.cpp:12:21: error: expected ';' at end of declaration
  • clang/test/CXX/lex/lex.literal/lex.ext/p10.cpp:13:33: error: expected ';' at end of declaration