Author: Shafik Yaghmour
Date: 2023-09-12T11:06:06-07:00
New Revision: 6eadc8f16e03f6aa3b1b1c178c308ac452eabeac

URL: 
https://github.com/llvm/llvm-project/commit/6eadc8f16e03f6aa3b1b1c178c308ac452eabeac
DIFF: 
https://github.com/llvm/llvm-project/commit/6eadc8f16e03f6aa3b1b1c178c308ac452eabeac.diff

LOG: [Clang] Fix crash in Parser::ParseDirectDeclarator by adding check that 
token is not an annotation token

In Parser::ParseDirectDeclarator(...) in some cases ill-formed code can cause an
annotation token to end up where it was not expected. The fix is to add a
!Tok.isAnnotation() guard before attempting to access identifier info.

This fixes: https://github.com/llvm/llvm-project/issues/64836

Differential Revision: https://reviews.llvm.org/D158804

Added: 
    clang/test/Parser/gh64836.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Parse/ParseDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 768b322ca5e721d..3cdad2f7b9f0e5a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -218,6 +218,8 @@ Bug Fixes in This Version
   (`#65156 <https://github.com/llvm/llvm-project/issues/65156>`_)
 - Clang no longer considers the loss of ``__unaligned`` qualifier from objects 
as
   an invalid conversion during method function overload resolution.
+- Fix parser crash when dealing with ill-formed objective C++ header code. 
Fixes
+  (`#64836 <https://github.com/llvm/llvm-project/issues/64836>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 7c27a02ee4af625..748b9d53c9f5b33 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -6667,7 +6667,7 @@ void Parser::ParseDirectDeclarator(Declarator &D) {
       // Objective-C++: Detect C++ keywords and try to prevent further errors 
by
       // treating these keyword as valid member names.
       if (getLangOpts().ObjC && getLangOpts().CPlusPlus &&
-          Tok.getIdentifierInfo() &&
+          !Tok.isAnnotation() && Tok.getIdentifierInfo() &&
           Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) {
         Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
              diag::err_expected_member_name_or_semi_objcxx_keyword)

diff  --git a/clang/test/Parser/gh64836.cpp b/clang/test/Parser/gh64836.cpp
new file mode 100644
index 000000000000000..12b726060cb3fd9
--- /dev/null
+++ b/clang/test/Parser/gh64836.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -xobjective-c++-header %s
+
+template <typename, typename>
+class C {};
+
+class B {
+  p // expected-error {{unknown type name 'p'}}
+ private: // expected-error {{'private' is a keyword in Objective-C++}}
+  void f() {} // expected-error {{expected '(' for function-style cast or type 
construction}}
+  C<int, decltype(f)> c; // expected-error {{use of undeclared identifier 'f'}}
+  // expected-error@-1 {{expected member name}}
+};


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to