Skip to content

Conversation

@mstorsjo
Copy link
Member

This fixes the following kind of warning when compiled with GCC:

../../clang/lib/Parse/ParseStmt.cpp: In member function ‘clang::StmtResult clang::Parser::ParseCXXTryBlockCommon(clang::SourceLocation, bool)’:
../../clang/lib/Parse/ParseStmt.cpp:2534:40: warning: enumerated and non-enumerated type in conditional expression [-Wextra]
 2534 |                                 (FnTry ? Scope::FnTryCatchScope : 0)));
      |                                  ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Use Scope::NoScope instead of literal 0 as fallback in the ternary expressions.

…ssions. NFC.

This fixes the following kind of warning when compiled with GCC:

    ../../clang/lib/Parse/ParseStmt.cpp: In member function ‘clang::StmtResult clang::Parser::ParseCXXTryBlockCommon(clang::SourceLocation, bool)’:
    ../../clang/lib/Parse/ParseStmt.cpp:2534:40: warning: enumerated and non-enumerated type in conditional expression [-Wextra]
     2534 |                                 (FnTry ? Scope::FnTryCatchScope : 0)));
          |                                  ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Use `Scope::NoScope` instead of literal 0 as fallback in the
ternary expressions.
@mstorsjo mstorsjo requested a review from erichkeane September 17, 2025 11:48
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2025

@llvm/pr-subscribers-clang

Author: Martin Storsjö (mstorsjo)

Changes

This fixes the following kind of warning when compiled with GCC:

../../clang/lib/Parse/ParseStmt.cpp: In member function ‘clang::StmtResult clang::Parser::ParseCXXTryBlockCommon(clang::SourceLocation, bool)’:
../../clang/lib/Parse/ParseStmt.cpp:2534:40: warning: enumerated and non-enumerated type in conditional expression [-Wextra]
 2534 |                                 (FnTry ? Scope::FnTryCatchScope : 0)));
      |                                  ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Use Scope::NoScope instead of literal 0 as fallback in the ternary expressions.


Full diff: https://github.com/llvm/llvm-project/pull/159334.diff

3 Files Affected:

  • (modified) clang/lib/Parse/ParseDecl.cpp (+7-6)
  • (modified) clang/lib/Parse/ParseObjc.cpp (+3-3)
  • (modified) clang/lib/Parse/ParseStmt.cpp (+6-6)
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index bbeee2e3e373f..22c01c4e371f3 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -6813,10 +6813,10 @@ void Parser::ParseDirectDeclarator(Declarator &D) {
       bool IsFunctionDeclaration = D.isFunctionDeclaratorAFunctionDeclaration();
       // Enter function-declaration scope, limiting any declarators to the
       // function prototype scope, including parameter declarators.
-      ParseScope PrototypeScope(this,
-                                Scope::FunctionPrototypeScope|Scope::DeclScope|
-                                (IsFunctionDeclaration
-                                   ? Scope::FunctionDeclarationScope : 0));
+      ParseScope PrototypeScope(
+          this, Scope::FunctionPrototypeScope | Scope::DeclScope |
+                    (IsFunctionDeclaration ? Scope::FunctionDeclarationScope
+                                           : Scope::NoScope));
 
       // The paren may be part of a C++ direct initializer, eg. "int x(1);".
       // In such a case, check if we actually have a function declarator; if it
@@ -7097,8 +7097,9 @@ void Parser::ParseParenDeclarator(Declarator &D) {
   // function prototype scope, including parameter declarators.
   ParseScope PrototypeScope(this,
                             Scope::FunctionPrototypeScope | Scope::DeclScope |
-                            (D.isFunctionDeclaratorAFunctionDeclaration()
-                               ? Scope::FunctionDeclarationScope : 0));
+                                (D.isFunctionDeclaratorAFunctionDeclaration()
+                                     ? Scope::FunctionDeclarationScope
+                                     : Scope::NoScope));
   ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
   PrototypeScope.Exit();
 }
diff --git a/clang/lib/Parse/ParseObjc.cpp b/clang/lib/Parse/ParseObjc.cpp
index 291c70e7bad4b..a64fb02294c5a 100644
--- a/clang/lib/Parse/ParseObjc.cpp
+++ b/clang/lib/Parse/ParseObjc.cpp
@@ -3295,9 +3295,9 @@ void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
   assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
          "Inline objective-c method not starting with '{' or 'try' or ':'");
   // Enter a scope for the method or c-function body.
-  ParseScope BodyScope(this, (parseMethod ? Scope::ObjCMethodScope : 0) |
-                                 Scope::FnScope | Scope::DeclScope |
-                                 Scope::CompoundStmtScope);
+  ParseScope BodyScope(
+      this, (parseMethod ? Scope::ObjCMethodScope : Scope::NoScope) |
+                Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope);
   Sema::FPFeaturesStateRAII SaveFPFeatures(Actions);
 
   // Tell the actions module that we have entered a method or c-function definition
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 62361c066a3f3..2e7af1219547e 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -2529,9 +2529,9 @@ StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
     return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
 
   StmtResult TryBlock(ParseCompoundStatement(
-      /*isStmtExpr=*/false, Scope::DeclScope | Scope::TryScope |
-                                Scope::CompoundStmtScope |
-                                (FnTry ? Scope::FnTryCatchScope : 0)));
+      /*isStmtExpr=*/false,
+      Scope::DeclScope | Scope::TryScope | Scope::CompoundStmtScope |
+          (FnTry ? Scope::FnTryCatchScope : Scope::NoScope)));
   if (TryBlock.isInvalid())
     return TryBlock;
 
@@ -2593,9 +2593,9 @@ StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
   // C++ 3.3.2p3:
   // The name in a catch exception-declaration is local to the handler and
   // shall not be redeclared in the outermost block of the handler.
-  ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
-                                  Scope::CatchScope |
-                                  (FnCatch ? Scope::FnTryCatchScope : 0));
+  ParseScope CatchScope(
+      this, Scope::DeclScope | Scope::ControlScope | Scope::CatchScope |
+                (FnCatch ? Scope::FnTryCatchScope : Scope::NoScope));
 
   // exception-declaration is equivalent to '...' or a parameter-declaration
   // without default arguments.

@mstorsjo
Copy link
Member Author

Admittedly, this isn't very pretty - the 0 as fallback default in the expressions is more readable indeed, but this looks like the simplest way to avoid warnings here (and this is a somewhat reasonable thing to warn about IMO).

@Fznamznon
Copy link
Contributor

FYI #159275

@mstorsjo
Copy link
Member Author

FYI #159275

Oh, what a coincidence.

Feel free to go with that PR - but it would be nice to amend that one with the concrete example of a warning that is being avoided.

@cor3ntin
Copy link
Contributor

Duplicate of #159275

@cor3ntin cor3ntin marked this as a duplicate of #159275 Sep 17, 2025
@cor3ntin cor3ntin closed this Sep 17, 2025
@mstorsjo mstorsjo deleted the gcc-mix-enum-ternary branch September 17, 2025 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants