Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ LANGOPT(CXXAssumptions, 1, 1, NotCompatible, "Enable or disable codegen and comp

LANGOPT(RawStringLiterals, 1, 1, NotCompatible, "Enable or disable raw string literals")

LANGOPT(AllowLiteralDigitSeparator, 1, 0, NotCompatible, "Allow literal digit seperator in source")

ENUM_LANGOPT(StrictFlexArraysLevel, StrictFlexArraysLevelKind, 2,
StrictFlexArraysLevelKind::Default, NotCompatible,
"Rely on strict definition of flexible arrays")
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/LangStandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ struct LangStandard {
return isCPlusPlus11() || (!isCPlusPlus() && isC99() && isGNUMode());
}

/// allowLiteralDigitSeparator - Language supports literal digit seperator
bool allowLiteralDigitSeparator() const { return isCPlusPlus14() || isC23(); }

/// isGNUMode - Language includes GNU extensions.
bool isGNUMode() const { return Flags & GNUMode; }

Expand Down
1 change: 1 addition & 0 deletions clang/lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ void LangOptions::setLangDefaults(LangOptions &Opts, Language Lang,
Opts.WChar = Std.isCPlusPlus();
Opts.Digraphs = Std.hasDigraphs();
Opts.RawStringLiterals = Std.hasRawStringLiterals();
Opts.AllowLiteralDigitSeparator = Std.allowLiteralDigitSeparator();
Opts.NamedLoops = Std.isC2y();

Opts.HLSL = Lang == Language::HLSL;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Lex/DependencyDirectivesScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct Scanner {
LangOpts.ObjC = true;
LangOpts.LineComment = true;
LangOpts.RawStringLiterals = true;
LangOpts.AllowLiteralDigitSeparator = true;
// FIXME: we do not enable C11 or C++11, so we are missing u/u8/U"".
return LangOpts;
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Lex/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2087,7 +2087,7 @@ bool Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
}

// If we have a digit separator, continue.
if (C == '\'' && (LangOpts.CPlusPlus14 || LangOpts.C23)) {
if (C == '\'' && LangOpts.AllowLiteralDigitSeparator) {
auto [Next, NextSize] = getCharAndSizeNoWarn(CurPtr + Size, LangOpts);
if (isAsciiIdentifierContinue(Next)) {
if (!isLexingRawMode())
Expand Down
16 changes: 16 additions & 0 deletions clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "clang/Lex/DependencyDirectivesScanner.h"
#include "clang/Basic/TokenKinds.h"
#include "llvm/ADT/SmallString.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -1000,6 +1001,21 @@ int z = 128'78;
EXPECT_STREQ("#include <test.h>\n", Out.data());
}

TEST(MinimizeSourceToDependencyDirectivesTest, CharacterLiteralInPreprocessor) {
SmallVector<char, 128> Out;
SmallVector<dependency_directives_scan::Token, 8> Tokens;
SmallVector<Directive, 4> Directives;

StringRef Source = R"(
#if 1'2 == 12
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder do we have the same problem w/ binary literals which was also brought in via C++14 e.g. 0b10 and then we have the z suffix which came in w/ C++23.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Lexer seems to be just leaving out these checks to Preprocessor and Sema. Maybe it's better to postpone the check for digit separators in this case, too?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was curious b/c this check was C++ version specific and was curious how other lexical features version related were handled.

CC @cor3ntin

#endif
)";
ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives));
ASSERT_GE(Tokens.size(), 4u);
EXPECT_EQ(Tokens[2].Kind, tok::numeric_constant);
EXPECT_EQ(Tokens[3].Kind, tok::equalequal);
}

TEST(MinimizeSourceToDependencyDirectivesTest, PragmaOnce) {
SmallVector<char, 128> Out;
SmallVector<dependency_directives_scan::Token, 4> Tokens;
Expand Down