[clang-tools-extra] 3ddfea0 - [clangd] Handle the C++2b elifdef and elindef PP structure in CollectMainFileMacros.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 29 02:24:59 PDT 2023
Author: Haojian Wu
Date: 2023-03-29T11:24:44+02:00
New Revision: 3ddfea07f8d033a7e78695baf282a54f0d6765e5
URL: https://github.com/llvm/llvm-project/commit/3ddfea07f8d033a7e78695baf282a54f0d6765e5
DIFF: https://github.com/llvm/llvm-project/commit/3ddfea07f8d033a7e78695baf282a54f0d6765e5.diff
LOG: [clangd] Handle the C++2b elifdef and elindef PP structure in CollectMainFileMacros.
Reviewed By: kadircet
Differential Revision: https://reviews.llvm.org/D146717
Added:
Modified:
clang-tools-extra/clangd/CollectMacros.cpp
clang-tools-extra/clangd/CollectMacros.h
clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/CollectMacros.cpp b/clang-tools-extra/clangd/CollectMacros.cpp
index c0ed8b68ea481..9fa3d6955fc6f 100644
--- a/clang-tools-extra/clangd/CollectMacros.cpp
+++ b/clang-tools-extra/clangd/CollectMacros.cpp
@@ -36,33 +36,51 @@ void CollectMainFileMacros::FileChanged(SourceLocation Loc, FileChangeReason,
SrcMgr::CharacteristicKind, FileID) {
InMainFile = isInsideMainFile(Loc, SM);
}
+
void CollectMainFileMacros::MacroExpands(const Token &MacroName,
const MacroDefinition &MD,
SourceRange Range,
const MacroArgs *Args) {
add(MacroName, MD.getMacroInfo());
}
+
void CollectMainFileMacros::MacroUndefined(const clang::Token &MacroName,
const clang::MacroDefinition &MD,
const clang::MacroDirective *Undef) {
add(MacroName, MD.getMacroInfo());
}
+
void CollectMainFileMacros::Ifdef(SourceLocation Loc, const Token &MacroName,
const MacroDefinition &MD) {
add(MacroName, MD.getMacroInfo(), /*IsDefinition=*/false,
/*InConditionalDirective=*/true);
}
+
void CollectMainFileMacros::Ifndef(SourceLocation Loc, const Token &MacroName,
const MacroDefinition &MD) {
add(MacroName, MD.getMacroInfo(), /*IsDefinition=*/false,
/*InConditionalDirective=*/true);
}
+
+void CollectMainFileMacros::Elifdef(SourceLocation Loc, const Token &MacroName,
+ const MacroDefinition &MD) {
+ add(MacroName, MD.getMacroInfo(), /*IsDefinition=*/false,
+ /*InConditionalDirective=*/true);
+}
+
+void CollectMainFileMacros::Elifndef(SourceLocation Loc, const Token &MacroName,
+ const MacroDefinition &MD) {
+ add(MacroName, MD.getMacroInfo(), /*IsDefinition=*/false,
+ /*InConditionalDirective=*/true);
+}
+
void CollectMainFileMacros::Defined(const Token &MacroName,
const MacroDefinition &MD,
SourceRange Range) {
add(MacroName, MD.getMacroInfo(), /*IsDefinition=*/false,
/*InConditionalDirective=*/true);
}
+
void CollectMainFileMacros::SourceRangeSkipped(SourceRange R,
SourceLocation EndifLoc) {
if (!InMainFile)
diff --git a/clang-tools-extra/clangd/CollectMacros.h b/clang-tools-extra/clangd/CollectMacros.h
index d5789a2a88912..716a5d58070fd 100644
--- a/clang-tools-extra/clangd/CollectMacros.h
+++ b/clang-tools-extra/clangd/CollectMacros.h
@@ -12,6 +12,7 @@
#include "Protocol.h"
#include "SourceCode.h"
#include "index/SymbolID.h"
+#include "clang/Basic/SourceLocation.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/DenseMap.h"
@@ -61,11 +62,16 @@ class CollectMainFileMacros : public PPCallbacks {
const clang::MacroDefinition &MD,
const clang::MacroDirective *Undef) override;
- // FIXME: handle C++23 #elifdef, #elifndef
void Ifdef(SourceLocation Loc, const Token &MacroName,
const MacroDefinition &MD) override;
void Ifndef(SourceLocation Loc, const Token &MacroName,
const MacroDefinition &MD) override;
+ using PPCallbacks::Elifdef;
+ using PPCallbacks::Elifndef;
+ void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroDefinition &MD) override;
+ void Elifndef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroDefinition &MD) override;
void Defined(const Token &MacroName, const MacroDefinition &MD,
SourceRange Range) override;
diff --git a/clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp b/clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
index 163a7f1a31707..459d18aed07d6 100644
--- a/clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
@@ -56,6 +56,11 @@ TEST(CollectMainFileMacros, SelectedMacros) {
// )cpp",
R"cpp(
#ifdef $Unknown(condit)[[UNDEFINED]]
+ #elifdef $Unknown(condit)[[UNDEFINED]]
+ #endif
+
+ #ifdef $Unknown(condit)[[UNDEFINED]]
+ #elifndef $Unknown(condit)[[UNDEFINED]]
#endif
#ifndef $Unknown(condit)[[UNDEFINED]]
@@ -101,11 +106,12 @@ TEST(CollectMainFileMacros, SelectedMacros) {
for (const char *Test : Tests) {
Annotations T(Test);
- auto AST = TestTU::withCode(T.code()).build();
+ auto Inputs = TestTU::withCode(T.code());
+ Inputs.ExtraArgs.push_back("-std=c++2b");
+ auto AST = Inputs.build();
auto ActualMacroRefs = AST.getMacros();
auto &SM = AST.getSourceManager();
auto &PP = AST.getPreprocessor();
-
for (const auto &[Name, Ranges] : T.all_ranges()) {
if (Name == "Unknown") {
EXPECT_THAT(ActualMacroRefs.UnknownMacros,
More information about the cfe-commits
mailing list