[clang] clang: Relax LangOpts checks when lexing quoted numbers during preprocessing (PR #95798)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 17 07:52:09 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Nishith Kumar M Shah (nishithshah2211)
<details>
<summary>Changes</summary>
When trying to lex numeric constants that use single quotes as separators, if the lexer is parsing a preprocessor directive, we can relax the language options check. These checks will be enforced in a later phase after preprocessing/scanning.
This commit is a second attempt to fix
https://github.com/llvm/llvm-project/issues/88896. The first attempt to fix this is here: https://github.com/llvm/llvm-project/pull/93753, which had to be reverted because of the discussions in that same PR.
---
Full diff: https://github.com/llvm/llvm-project/pull/95798.diff
2 Files Affected:
- (modified) clang/lib/Lex/Lexer.cpp (+2-1)
- (modified) clang/unittests/Lex/DependencyDirectivesScannerTest.cpp (+29)
``````````diff
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index e59c7805b3862..60c101871be75 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -2068,7 +2068,8 @@ bool Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
}
// If we have a digit separator, continue.
- if (C == '\'' && (LangOpts.CPlusPlus14 || LangOpts.C23)) {
+ if (C == '\'' &&
+ (LangOpts.CPlusPlus14 || LangOpts.C23 || ParsingPreprocessorDirective)) {
auto [Next, NextSize] = getCharAndSizeNoWarn(CurPtr + Size, LangOpts);
if (isAsciiIdentifierContinue(Next)) {
if (!isLexingRawMode())
diff --git a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
index 59fef9ecbb9c9..0d2d98b16f350 100644
--- a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
+++ b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "clang/Lex/DependencyDirectivesScanner.h"
+#include "clang/Basic/TokenKinds.h"
#include "llvm/ADT/SmallString.h"
#include "gtest/gtest.h"
@@ -1012,4 +1013,32 @@ TEST(MinimizeSourceToDependencyDirectivesTest, TokensBeforeEOF) {
EXPECT_STREQ("#ifndef A\n#define A\n#endif\n<TokBeforeEOF>\n", Out.data());
}
+TEST(MinimizeSourceToDependencyDirectivesTest, QuoteSeparatedNumber) {
+ SmallVector<char, 128> Out;
+ SmallVector<dependency_directives_scan::Token, 4> Tokens;
+ SmallVector<Directive, 4> Directives;
+
+ StringRef Source = R"(
+#if 123'124
+#endif
+)";
+
+ ASSERT_FALSE(
+ minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives));
+ EXPECT_STREQ("#if 123'124\n#endif\n", Out.data());
+ ASSERT_EQ(Directives.size(), 3u);
+ EXPECT_EQ(Directives[0].Kind, dependency_directives_scan::pp_if);
+ EXPECT_EQ(Directives[1].Kind, dependency_directives_scan::pp_endif);
+ EXPECT_EQ(Directives[2].Kind, dependency_directives_scan::pp_eof);
+ ASSERT_EQ(Tokens.size(), 7u);
+
+ ASSERT_TRUE(Tokens[0].is(tok::hash));
+ ASSERT_TRUE(Tokens[1].is(tok::raw_identifier)); // "if"
+ ASSERT_TRUE(Tokens[2].is(tok::numeric_constant)); // 123'124
+ ASSERT_TRUE(Tokens[3].is(tok::eod));
+ ASSERT_TRUE(Tokens[4].is(tok::hash));
+ ASSERT_TRUE(Tokens[5].is(tok::raw_identifier)); // #endif
+ ASSERT_TRUE(Tokens[6].is(tok::eod));
+}
+
} // end anonymous namespace
``````````
</details>
https://github.com/llvm/llvm-project/pull/95798
More information about the cfe-commits
mailing list