From ff6e4441b93953efb2c52995e79e211a49ffac06 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Wed, 7 Oct 2020 16:29:22 +0100 Subject: [PATCH] [clang-format][tests] Fix MacroExpander lexer not parsing C++ keywords While debugging a different clang-format failure, I tried to reuse the MacroExpander lexer, but was surprised to see that it marks all C++ keywords (e.g. const, decltype) as being of type identifier. After stepping through the ::format() code, I noticed that the difference between these two is that the identifier table was not being initialized based on the FormatStyle, so only basic tokens such as tok::semi, tok::plus, etc. were being handled. Reviewed By: klimek Differential Revision: https://reviews.llvm.org/D88952 --- clang/unittests/Format/MacroExpanderTest.cpp | 16 ++++++++++++++++ clang/unittests/Format/TestLexer.h | 6 ++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/clang/unittests/Format/MacroExpanderTest.cpp b/clang/unittests/Format/MacroExpanderTest.cpp index 59c67f29bedd..20e1dba0d49a 100644 --- a/clang/unittests/Format/MacroExpanderTest.cpp +++ b/clang/unittests/Format/MacroExpanderTest.cpp @@ -182,6 +182,22 @@ TEST_F(MacroExpanderTest, SingleExpansion) { EXPECT_ATTRIBUTES(Result, Attributes); } +TEST_F(MacroExpanderTest, UnderstandsCppTokens) { + auto Macros = create({"A(T,name)=T name = 0;"}); + auto *A = Lex.id("A"); + auto Args = lexArgs({"const int", "x"}); + auto Result = uneof(Macros->expand(A, Args)); + std::vector Attributes = { + {tok::kw_const, MR_ExpandedArg, 1, 0, {A}}, + {tok::kw_int, MR_ExpandedArg, 0, 0, {A}}, + {tok::identifier, MR_ExpandedArg, 0, 0, {A}}, + {tok::equal, MR_Hidden, 0, 0, {A}}, + {tok::numeric_constant, MR_Hidden, 0, 0, {A}}, + {tok::semi, MR_Hidden, 0, 1, {A}}, + }; + EXPECT_ATTRIBUTES(Result, Attributes); +} + } // namespace } // namespace format } // namespace clang diff --git a/clang/unittests/Format/TestLexer.h b/clang/unittests/Format/TestLexer.h index 8c5eb2b029fb..2b56f10dd379 100644 --- a/clang/unittests/Format/TestLexer.h +++ b/clang/unittests/Format/TestLexer.h @@ -55,7 +55,9 @@ inline std::string text(llvm::ArrayRef Tokens) { class TestLexer { public: - TestLexer() : SourceMgr("test.cpp", "") {} + TestLexer(FormatStyle Style = getLLVMStyle()) + : Style(Style), SourceMgr("test.cpp", ""), + IdentTable(getFormattingLangOpts(Style)) {} TokenList lex(llvm::StringRef Code) { Buffers.push_back( @@ -74,7 +76,7 @@ public: return Result[0]; } - FormatStyle Style = getLLVMStyle(); + FormatStyle Style; encoding::Encoding Encoding = encoding::Encoding_UTF8; std::vector> Buffers; clang::SourceManagerForFile SourceMgr;