Skip to content

[clang-format] Fix a crash in QualifierAlignment: Right #120821

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

Merged
merged 1 commit into from
Dec 22, 2024
Merged

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Dec 21, 2024

Fixes #120793.

@llvmbot
Copy link
Member

llvmbot commented Dec 21, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixes #120793.


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

2 Files Affected:

  • (modified) clang/lib/Format/QualifierAlignmentFixer.cpp (+1-1)
  • (modified) clang/unittests/Format/QualifierFixerTest.cpp (+5)
diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index 593f8efff25aa9..530b2dd538cee0 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -348,7 +348,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
       }
     }
 
-    if (Next->is(tok::kw_auto))
+    if (Next && Next->is(tok::kw_auto))
       TypeToken = Next;
 
     // Place the Qualifier at the end of the list of qualifiers.
diff --git a/clang/unittests/Format/QualifierFixerTest.cpp b/clang/unittests/Format/QualifierFixerTest.cpp
index f9255c6e4c7088..9ed567445eb07e 100644
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -531,6 +531,11 @@ TEST_F(QualifierFixerTest, RightQualifier) {
   verifyFormat("float (C::*const p)(int);", Style);
   verifyFormat("float (C::*p)(int) const;", Style);
   verifyFormat("float const (C::*p)(int);", "const float (C::*p)(int);", Style);
+
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.AfterFunction = true;
+  verifyFormat("auto foo() -> T const { return bar; }",
+               "auto foo() -> const T { return bar; }", Style);
 }
 
 TEST_F(QualifierFixerTest, LeftQualifier) {

@owenca owenca merged commit 40acaa3 into llvm:main Dec 22, 2024
10 checks passed
@owenca owenca deleted the 120793 branch December 22, 2024 01:16
qiaojbao pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Feb 7, 2025
Local branch amd-gfx abc91de Merged main:bc23ef3feba9 into amd-gfx:0060b1beee69
Remote branch main 40acaa3 [clang-format] Fix a crash in `QualifierAlignment: Right` (llvm#120821)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-format] Crash in LeftRightQualifierAlignmentFixer for trailing return type
3 participants