[clang] [clang] Improved isSimpleTypeSpecifier (PR #79037)

Carl Peto via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 26 07:35:20 PST 2024


https://github.com/carlos4242 updated https://github.com/llvm/llvm-project/pull/79037

>From fdc1e0dbec6821e292ba3da7770dbae22923db20 Mon Sep 17 00:00:00 2001
From: Carl Peto <CPeto at becrypt.com>
Date: Fri, 26 Jan 2024 14:20:48 +0000
Subject: [PATCH]     [clang]     - Sema::isSimpleTypeSpecifier return true for
 _Bool in c99 (currently returns false for _Bool, regardless of C dialect).
 (Fixes #72203)     - replace the logic with a check for simple types and a
 proper check for a valid keyword in the appropriate dialect

---
 clang/include/clang/Lex/Token.h |  7 +++++++
 clang/include/clang/Sema/Sema.h |  2 +-
 clang/lib/Parse/ParseExpr.cpp   |  2 +-
 clang/lib/Parse/ParseObjc.cpp   |  2 +-
 clang/lib/Sema/SemaDecl.cpp     | 26 +++++++++++++-------------
 5 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/clang/include/clang/Lex/Token.h b/clang/include/clang/Lex/Token.h
index 1409e2c58b550d..3c707d46932213 100644
--- a/clang/include/clang/Lex/Token.h
+++ b/clang/include/clang/Lex/Token.h
@@ -196,6 +196,13 @@ class Token {
     PtrData = (void*) II;
   }
 
+  bool hasIdentifierInfo() {
+    if (is(tok::raw_identifier) || isAnnotation() || isLiteral() ||
+        is(tok::eof))
+      return false;
+    return true;
+  }
+
   const void *getEofData() const {
     assert(is(tok::eof));
     return reinterpret_cast<const void *>(PtrData);
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1f1cbd11ff7358..dc8797b2f12106 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -2636,7 +2636,7 @@ class Sema final {
 
   void DiagnoseUseOfUnimplementedSelectors();
 
-  bool isSimpleTypeSpecifier(tok::TokenKind Kind) const;
+  bool isSimpleTypeSpecifier(Token &Tok) const;
 
   ParsedType getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
                          Scope *S, CXXScopeSpec *SS = nullptr,
diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp
index e862856a08ca11..034d56c6b0e300 100644
--- a/clang/lib/Parse/ParseExpr.cpp
+++ b/clang/lib/Parse/ParseExpr.cpp
@@ -1597,7 +1597,7 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind,
       if (TryAnnotateTypeOrScopeToken())
         return ExprError();
 
-      if (!Actions.isSimpleTypeSpecifier(Tok.getKind()))
+      if (!Actions.isSimpleTypeSpecifier(Tok))
         // We are trying to parse a simple-type-specifier but might not get such
         // a token after error recovery.
         return ExprError();
diff --git a/clang/lib/Parse/ParseObjc.cpp b/clang/lib/Parse/ParseObjc.cpp
index 849fd1ac95a442..4771b69eadb34b 100644
--- a/clang/lib/Parse/ParseObjc.cpp
+++ b/clang/lib/Parse/ParseObjc.cpp
@@ -2971,7 +2971,7 @@ bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
                   tok::annot_cxxscope))
     TryAnnotateTypeOrScopeToken();
 
-  if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
+  if (!Actions.isSimpleTypeSpecifier(Tok)) {
     //   objc-receiver:
     //     expression
     // Make sure any typos in the receiver are corrected or diagnosed, so that
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f9bf1d14bdc4f6..4c7b12a53768c0 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -128,10 +128,15 @@ class TypeNameValidatorCCC final : public CorrectionCandidateCallback {
 } // end anonymous namespace
 
 /// Determine whether the token kind starts a simple-type-specifier.
-bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const {
+bool Sema::isSimpleTypeSpecifier(Token &Tok) const {
+  auto Kind = Tok.getKind();
+  auto LangOpts = getLangOpts();
+
   switch (Kind) {
-  // FIXME: Take into account the current language when deciding whether a
-  // token kind is a valid type specifier
+  case tok::annot_typename:
+  case tok::annot_decltype:
+    return true;
+
   case tok::kw_short:
   case tok::kw_long:
   case tok::kw___int64:
@@ -156,24 +161,19 @@ bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const {
 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
 #include "clang/Basic/TransformTypeTraits.def"
   case tok::kw___auto_type:
-    return true;
-
-  case tok::annot_typename:
+  case tok::kw__Bool:
   case tok::kw_char16_t:
   case tok::kw_char32_t:
   case tok::kw_typeof:
-  case tok::annot_decltype:
   case tok::kw_decltype:
-    return getLangOpts().CPlusPlus;
-
   case tok::kw_char8_t:
-    return getLangOpts().Char8;
+    if (!Tok.hasIdentifierInfo())
+      return false;
+    return Tok.getIdentifierInfo()->isKeyword(LangOpts);
 
   default:
-    break;
+    return false;
   }
-
-  return false;
 }
 
 namespace {



More information about the cfe-commits mailing list