[clang] [clang] Improved isSimpleTypeSpecifier (PR #79037)
Carl Peto via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 28 12:32:32 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 1/2] [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 1409e2c58b550df..3c707d469322139 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 1f1cbd11ff73581..dc8797b2f12106f 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 e862856a08ca117..034d56c6b0e300a 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 849fd1ac95a442e..4771b69eadb34b9 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 f9bf1d14bdc4f68..4c7b12a53768c01 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 {
>From 986324450006ad6dc761e85e7611a6e93a4ee2fb Mon Sep 17 00:00:00 2001
From: Carl Peto <CPeto at becrypt.com>
Date: Sun, 28 Jan 2024 20:32:17 +0000
Subject: [PATCH 2/2] PR feedback fixes for owenca
---
clang/include/clang/Lex/Token.h | 7 -------
clang/include/clang/Sema/Sema.h | 2 +-
clang/lib/Sema/SemaDecl.cpp | 13 ++++---------
3 files changed, 5 insertions(+), 17 deletions(-)
diff --git a/clang/include/clang/Lex/Token.h b/clang/include/clang/Lex/Token.h
index 3c707d469322139..1409e2c58b550df 100644
--- a/clang/include/clang/Lex/Token.h
+++ b/clang/include/clang/Lex/Token.h
@@ -196,13 +196,6 @@ 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 dc8797b2f12106f..91d5a5646f95f37 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(Token &Tok) const;
+ bool isSimpleTypeSpecifier(const Token &Tok) const;
ParsedType getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
Scope *S, CXXScopeSpec *SS = nullptr,
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 4c7b12a53768c01..63afda2be76711f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -128,11 +128,8 @@ class TypeNameValidatorCCC final : public CorrectionCandidateCallback {
} // end anonymous namespace
/// Determine whether the token kind starts a simple-type-specifier.
-bool Sema::isSimpleTypeSpecifier(Token &Tok) const {
- auto Kind = Tok.getKind();
- auto LangOpts = getLangOpts();
-
- switch (Kind) {
+bool Sema::isSimpleTypeSpecifier(const Token &Tok) const {
+ switch (Tok.getKind()) {
case tok::annot_typename:
case tok::annot_decltype:
return true;
@@ -155,21 +152,19 @@ bool Sema::isSimpleTypeSpecifier(Token &Tok) const {
case tok::kw___ibm128:
case tok::kw_wchar_t:
case tok::kw_bool:
+ case tok::kw__Bool:
case tok::kw__Accum:
case tok::kw__Fract:
case tok::kw__Sat:
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
#include "clang/Basic/TransformTypeTraits.def"
case tok::kw___auto_type:
- case tok::kw__Bool:
case tok::kw_char16_t:
case tok::kw_char32_t:
case tok::kw_typeof:
case tok::kw_decltype:
case tok::kw_char8_t:
- if (!Tok.hasIdentifierInfo())
- return false;
- return Tok.getIdentifierInfo()->isKeyword(LangOpts);
+ return Tok.getIdentifierInfo()->isKeyword(getLangOpts());
default:
return false;
More information about the cfe-commits
mailing list