llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Oleksandr Tarasiuk (a-tarasyuk)

<details>
<summary>Changes</summary>

Fixes #<!-- -->178098

---

This patch addressed the issue when `__has_cpp_attribute` is expanded with 
incomplete scoped attributes. The scoped name parsing can lex to `eof`/`eod` at

https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1877-L1881

and then proceed with

https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1425-L1430


since `eof`/`eod` is not guarded at

https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1367-L1372

this could lead to a preprocessor crash

https://github.com/llvm/llvm-project/blob/3f5a5d45d18a514f086f3e07c9676ca5fb95bbe9/clang/lib/Lex/PPMacroExpansion.cpp#L1370


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


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+1) 
- (modified) clang/lib/Lex/PPMacroExpansion.cpp (+1-1) 
- (modified) clang/test/Preprocessor/has_attribute_errors.cpp (+10) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d03211a200a29..a1a97492f6783 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -175,6 +175,7 @@ Bug Fixes in This Version
 - Fixed a failed assertion in the preprocessor when ``__has_embed`` parameters 
are missing parentheses. (#GH175088)
 
 - Fix lifetime extension of temporaries in for-range-initializers in 
templates. (#GH165182)
+- Fixed a preprocessor crash in ``__has_cpp_attribute`` on incomplete scoped 
attributes. (#GH178098)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp 
b/clang/lib/Lex/PPMacroExpansion.cpp
index 5efa4b5b3f872..734969e46e400 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1364,7 +1364,7 @@ static void 
EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS,
 
   Token ResultTok;
   bool SuppressDiagnostic = false;
-  while (true) {
+  while (Tok.isNoneOf(tok::eod, tok::eof)) {
     // Parse next token.
     if (ExpandArgs)
       PP.Lex(Tok);
diff --git a/clang/test/Preprocessor/has_attribute_errors.cpp 
b/clang/test/Preprocessor/has_attribute_errors.cpp
index 1fc88d3f926fb..d7492ed3f4a2c 100644
--- a/clang/test/Preprocessor/has_attribute_errors.cpp
+++ b/clang/test/Preprocessor/has_attribute_errors.cpp
@@ -14,3 +14,13 @@ __has_cpp_attribute(__clang__::fallthrough) // 
expected-error {{missing ')' afte
                                             // expected-note {{to match this 
'('}} \
                                             // expected-error {{builtin 
feature check macro requires a parenthesized identifier}}
 
+namespace GH178098 {
+// expected-error@+2 {{builtin feature check macro requires a parenthesized 
identifier}}
+// expected-error@+1 {{expected value in expression}}
+#if __has_cpp_attribute(clang::
+#endif
+
+// expected-error@+3 {{builtin feature check macro requires a parenthesized 
identifier}}
+// expected-error@+2 {{unterminated function-like macro invocation}}
+__has_cpp_attribute(clang::
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/178273
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to