Skip to content

Commit e03585e

Browse files
committed
[lldb] Re-use clang's keyword enable/disable mechanism in CPlusPlusNameParser.cpp
The problem is that CPlusPlusNameParser doesn't respect the language dialect for `target variable Class::constant`. If `constant` is a keyword in some (downstream in this case) dialects then it lexes `constant` as `kw_constant` instead of `kw_raw_identifier` even if that dialect is not active. As a result: * `target variable constant` works * `target variable Class::constant` fails to look up constant because it sees `kw_raw_identifier, kw_coloncolon, kw_constant` instead of `kw_raw_identifier, kw_coloncolon, kw_raw_identifier` * `expr -l c++ -- Class::constant` works because clang is parsing it Note: There's seemingly a separate bug where GetLangOptions() does not account for the process/frame language and just uses a pre-defined set of language options. I have not attempted to fix that since, for now, at least hardcoding my dialect to be disabled by that function does the right thing for existing tests. Also fixed a trivial return-after-else that clangd pointed out in the same area
1 parent 7af1e87 commit e03585e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

lldb/source/Plugins/Language/CPlusPlus/CPlusPlusNameParser.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,8 @@ CPlusPlusNameParser::ParseFullNameImpl() {
719719
}
720720
start_position.Remove();
721721
return result;
722-
} else {
723-
return std::nullopt;
724722
}
723+
return std::nullopt;
725724
}
726725

727726
llvm::StringRef CPlusPlusNameParser::GetTextForRange(const Range &range) {
@@ -755,12 +754,19 @@ static const clang::LangOptions &GetLangOptions() {
755754
return g_options;
756755
}
757756

758-
static const llvm::StringMap<tok::TokenKind> &GetKeywordsMap() {
759-
static llvm::StringMap<tok::TokenKind> g_map{
760-
#define KEYWORD(Name, Flags) {llvm::StringRef(#Name), tok::kw_##Name},
757+
static const llvm::StringMap<tok::TokenKind> GetKeywordsMap() {
758+
using namespace clang;
759+
llvm::StringMap<tok::TokenKind> g_map;
760+
761+
auto LangOpts = GetLangOptions();
762+
763+
KeywordStatus AddResult;
764+
#define KEYWORD(NAME, FLAGS) \
765+
AddResult = getKeywordStatus(LangOpts, FLAGS); \
766+
if (AddResult != KS_Disabled) \
767+
g_map[llvm::StringRef(#NAME)] = tok::kw_##NAME;
761768
#include "clang/Basic/TokenKinds.def"
762769
#undef KEYWORD
763-
};
764770
return g_map;
765771
}
766772

@@ -769,7 +775,7 @@ void CPlusPlusNameParser::ExtractTokens() {
769775
return;
770776
clang::Lexer lexer(clang::SourceLocation(), GetLangOptions(), m_text.data(),
771777
m_text.data(), m_text.data() + m_text.size());
772-
const auto &kw_map = GetKeywordsMap();
778+
const auto kw_map = GetKeywordsMap();
773779
clang::Token token;
774780
for (lexer.LexFromRawLexer(token); !token.is(clang::tok::eof);
775781
lexer.LexFromRawLexer(token)) {

0 commit comments

Comments
 (0)