Skip to content

[clang][NFC] Use switch in LoopHintAttr::getValueString #147119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bv2k4
Copy link
Contributor

@bv2k4 bv2k4 commented Jul 4, 2025

Replace if-else chain with switch over enum to improve code maintainability.

Copy link

github-actions bot commented Jul 4, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 4, 2025

@llvm/pr-subscribers-clang

Author: Bogdan Vetrenko (bv2k4)

Changes

Replace if-else chain with switch over enum.


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

1 Files Affected:

  • (modified) clang/lib/AST/AttrImpl.cpp (+25-12)
diff --git a/clang/lib/AST/AttrImpl.cpp b/clang/lib/AST/AttrImpl.cpp
index 5875a925d3fb0..c7618e1483e6f 100644
--- a/clang/lib/AST/AttrImpl.cpp
+++ b/clang/lib/AST/AttrImpl.cpp
@@ -41,25 +41,38 @@ std::string LoopHintAttr::getValueString(const PrintingPolicy &Policy) const {
   std::string ValueName;
   llvm::raw_string_ostream OS(ValueName);
   OS << "(";
-  if (state == Numeric)
+  switch (state) {
+  case Numeric:
     value->printPretty(OS, nullptr, Policy);
-  else if (state == FixedWidth || state == ScalableWidth) {
+    break;
+  case FixedWidth:
     if (value) {
       value->printPretty(OS, nullptr, Policy);
-      if (state == ScalableWidth)
-        OS << ", scalable";
-    } else if (state == ScalableWidth)
-      OS << "scalable";
-    else
+    } else {
       OS << "fixed";
-  } else if (state == Enable)
+    }
+    break;
+  case ScalableWidth:
+    if (value) {
+      value->printPretty(OS, nullptr, Policy);
+      OS << ", scalable";
+    } else {
+      OS << "scalable";
+    }
+    break;
+  case Enable:
     OS << "enable";
-  else if (state == Full)
+    break;
+  case Full:
     OS << "full";
-  else if (state == AssumeSafety)
+    break;
+  case AssumeSafety:
     OS << "assume_safety";
-  else
+    break;
+  case Disable:
     OS << "disable";
+    break;
+  }
   OS << ")";
   return ValueName;
 }
@@ -195,7 +208,7 @@ OMPDeclareTargetDeclAttr::getLocation(const ValueDecl *VD) {
 namespace clang {
 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo &TI);
 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo *TI);
-}
+} // namespace clang
 
 void OMPDeclareVariantAttr::printPrettyPragma(
     raw_ostream &OS, const PrintingPolicy &Policy) const {

@bv2k4 bv2k4 force-pushed the clang-loophint-switch branch from dcc061a to 9d8f8dc Compare July 5, 2025 00:13
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.

2 participants