Clang error: extraneous 'A' before ';' (err_extraneous_token_before_semi)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: extraneous 'A' before ';'
Type Error
Category Parse Issue
Internal Id err_extraneous_token_before_semi
Internal Message extraneous '%0' before ';'
Regular Expression (?:error|fatal error)\: extraneous '(.*?)' before ';'
First Commit 2010-09-07 45d6bdfa88d5 Improve recovery when there is a stray ']' or ')' before the ';' at

Description

The error is issued by the Clang compiler when it encounters an unexpected character token immediately before a semicolon (';') in the source code. This typically indicates the presence of an extraneous character that does not belong in the context where the error is reported. Common examples include a stray closing parenthesis ')' or a closing square bracket ']', positioned where a semicolon is expected to terminate a statement. Such extraneous tokens usually result from typographical mistakes or incorrect placement of characters during the coding process. Clang provides a diagnostic message indicating the nature of the unexpected token and suggests the removal of the extraneous character to resolve the issue.  
AI Generated

Example

In the following example, an error is depicted as originating from an unexpected ')' character found right before a semicolon in a C++ program. The example portrays a simple main function, within which an attempt to assign a value of 0 to the variable 'x' is made. However, the line intended for the assignment operation inadvertently includes an extra closing parenthesis immediately before the semicolon that terminates the statement. This inclusion of an extraneous ')' leads to a compilation error, signaling that the compiler has identified an unexpected token in what should have been a straightforward assignment statement. The compiler's response to this scenario is to issue a diagnostic message that pinpoints the nature of the syntax issue, advising on the removal of the unnecessary character to rectify the code's flaw.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
// Extraneous ')' before ';'
int main() { int x = 0); return 0; }
Compiler Output
<source>:2:23: error: extraneous ')' before ';'


Clang Internals (17.0.6)

Git Commit Message

Improve recovery when there is a stray ']' or ')' before the ';' at
the end of a statement. Fixes <rdar://problem/6896493>.

llvm-svn: 113202

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/Parser.cpp (line 168)

bool Parser::ExpectAndConsumeSemi(unsigned DiagID, StringRef TokenUsed) {
  // ...
  if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) && NextToken().is(tok::semi)) {
    Diag(Tok, diag::err_extraneous_token_before_semi) << PP.getSpelling(Tok) << FixItHint::CreateRemoval(Tok.getLocation());

Triggered in Clang Tests

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

clang/test/Parser/declarators.c

  • clang/test/Parser/declarators.c:110:19: error: extraneous ')' before ';'