[PATCH] D146279: [clangd] Extend CollectMainFileMacros.

2023-03-23 Thread Haojian Wu via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG002c4b7b955b: [clangd] Extend CollectMainFileMacros. 
(authored by hokein).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146279/new/

https://reviews.llvm.org/D146279

Files:
  clang-tools-extra/clangd/CollectMacros.cpp
  clang-tools-extra/clangd/CollectMacros.h
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/Preamble.cpp
  clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -399,7 +399,7 @@
   #define $Macro_decl[[MACRO_CONCAT]](X, V, T) T foo##X = V
   #define $Macro_decl[[DEF_VAR]](X, V) int X = V
   #define $Macro_decl[[DEF_VAR_T]](T, X, V) T X = V
-  #define $Macro_decl[[DEF_VAR_REV]](V, X) DEF_VAR(X, V)
+  #define $Macro_decl[[DEF_VAR_REV]](V, X) $Macro[[DEF_VAR]](X, V)
   #define $Macro_decl[[CPY]](X) X
   #define $Macro_decl[[DEF_VAR_TYPE]](X, Y) X Y
   #define $Macro_decl[[SOME_NAME]] variable
@@ -431,7 +431,7 @@
 )cpp",
   R"cpp(
   #define $Macro_decl[[fail]](expr) expr
-  #define $Macro_decl[[assert]](COND) if (!(COND)) { fail("assertion failed" #COND); }
+  #define $Macro_decl[[assert]](COND) if (!(COND)) { $Macro[[fail]]("assertion failed" #COND); }
   // Preamble ends.
   int $Variable_def[[x]];
   int $Variable_def[[y]];
Index: clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
===
--- clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
+++ clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
@@ -8,12 +8,14 @@
 #include "AST.h"
 #include "Annotations.h"
 #include "CollectMacros.h"
+#include "Matchers.h"
 #include "SourceCode.h"
 #include "TestTU.h"
 #include "clang/Basic/SourceLocation.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
+#include 
 
 namespace clang {
 namespace clangd {
@@ -21,19 +23,24 @@
 
 using testing::UnorderedElementsAreArray;
 
+MATCHER_P(rangeIs, R, "") { return arg.Rng == R; }
+MATCHER(isDef, "") { return arg.IsDefinition; }
+MATCHER(inConditionalDirective, "") { return arg.InConditionalDirective; }
+
 TEST(CollectMainFileMacros, SelectedMacros) {
   // References of the same symbol must have the ranges with the same
   // name(integer). If there are N different symbols then they must be named
   // from 1 to N. Macros for which SymbolID cannot be computed must be named
-  // "Unknown".
+  // "Unknown". The payload of the annotation describes the extra bit
+  // information of the MacroOccurrence (e.g. $1(def) => IsDefinition).
   const char *Tests[] = {
   R"cpp(// Macros: Cursor on definition.
-#define $1[[FOO]](x,y) (x + y)
+#define $1(def)[[FOO]](x,y) (x + y)
 int main() { int x = $1[[FOO]]($1[[FOO]](3, 4), $1[[FOO]](5, 6)); }
   )cpp",
   R"cpp(
-#define $1[[M]](X) X;
-#define $2[[abc]] 123
+#define $1(def)[[M]](X) X;
+#define $2(def)[[abc]] 123
 int s = $1[[M]]($2[[abc]]);
   )cpp",
   // FIXME: Locating macro in duplicate definitions doesn't work. Enable
@@ -48,31 +55,50 @@
   //   #undef $2[[abc]]
   // )cpp",
   R"cpp(
-#ifdef $Unknown[[UNDEFINED]]
+#ifdef $Unknown(condit)[[UNDEFINED]]
+#endif
+
+#ifndef $Unknown(condit)[[UNDEFINED]]
+#endif
+
+#if defined($Unknown(condit)[[UNDEFINED]])
 #endif
   )cpp",
   R"cpp(
-#ifndef $Unknown[[abc]]
-#define $1[[abc]]
-#ifdef $1[[abc]]
+#ifndef $Unknown(condit)[[abc]]
+#define $1(def)[[abc]]
+#ifdef $1(condit)[[abc]]
 #endif
 #endif
   )cpp",
   R"cpp(
 // Macros from token concatenations not included.
-#define $1[[CONCAT]](X) X##A()
-#define $2[[PREPEND]](X) MACRO##X()
-#define $3[[MACROA]]() 123
+#define $1(def)[[CONCAT]](X) X##A()
+#define $2(def)[[PREPEND]](X) MACRO##X()
+#define $3(def)[[MACROA]]() 123
 int B = $1[[CONCAT]](MACRO);
 int D = $2[[PREPEND]](A);
   )cpp",
   R"cpp(
-// FIXME: Macro names in a definition are not detected.
-#define $1[[MACRO_ARGS2]](X, Y) X Y
-#define $2[[FOO]] BAR
-#define $3[[BAR]] 1
+#define $1(def)[[MACRO_ARGS2]](X, Y) X Y
+#define $3(def)[[BAR]] 1
+#define $2(def)[[FOO]] $3[[BAR]]
 int A = $2[[FOO]];
   )cpp"};
+  auto ExpectedResults = [](const 

[PATCH] D146279: [clangd] Extend CollectMainFileMacros.

2023-03-23 Thread Haojian Wu via Phabricator via cfe-commits
hokein added inline comments.



Comment at: clang-tools-extra/clangd/CollectMacros.h:48
 public:
-  explicit CollectMainFileMacros(const SourceManager , MainFileMacros )
-  : SM(SM), Out(Out) {}
+  explicit CollectMainFileMacros(const SourceManager ,
+ const Preprocessor , MainFileMacros )

kadircet wrote:
> nit: you can get SM out of PP, no need to pass both.
oh, right!



Comment at: clang-tools-extra/clangd/CollectMacros.h:70
 
   void Ifdef(SourceLocation Loc, const Token ,
  const MacroDefinition ) override {

kadircet wrote:
> nit: can you actually move macro bodies out-of-line ? it's unfortunate that 
> we need to build half of the project everytime we touch this header.
Done (thanks for the define-out-line code action!)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146279/new/

https://reviews.llvm.org/D146279

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D146279: [clangd] Extend CollectMainFileMacros.

2023-03-23 Thread Haojian Wu via Phabricator via cfe-commits
hokein updated this revision to Diff 507679.
hokein marked an inline comment as done.
hokein added a comment.

address comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146279/new/

https://reviews.llvm.org/D146279

Files:
  clang-tools-extra/clangd/CollectMacros.cpp
  clang-tools-extra/clangd/CollectMacros.h
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/Preamble.cpp
  clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -399,7 +399,7 @@
   #define $Macro_decl[[MACRO_CONCAT]](X, V, T) T foo##X = V
   #define $Macro_decl[[DEF_VAR]](X, V) int X = V
   #define $Macro_decl[[DEF_VAR_T]](T, X, V) T X = V
-  #define $Macro_decl[[DEF_VAR_REV]](V, X) DEF_VAR(X, V)
+  #define $Macro_decl[[DEF_VAR_REV]](V, X) $Macro[[DEF_VAR]](X, V)
   #define $Macro_decl[[CPY]](X) X
   #define $Macro_decl[[DEF_VAR_TYPE]](X, Y) X Y
   #define $Macro_decl[[SOME_NAME]] variable
@@ -431,7 +431,7 @@
 )cpp",
   R"cpp(
   #define $Macro_decl[[fail]](expr) expr
-  #define $Macro_decl[[assert]](COND) if (!(COND)) { fail("assertion failed" #COND); }
+  #define $Macro_decl[[assert]](COND) if (!(COND)) { $Macro[[fail]]("assertion failed" #COND); }
   // Preamble ends.
   int $Variable_def[[x]];
   int $Variable_def[[y]];
Index: clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
===
--- clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
+++ clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
@@ -8,12 +8,14 @@
 #include "AST.h"
 #include "Annotations.h"
 #include "CollectMacros.h"
+#include "Matchers.h"
 #include "SourceCode.h"
 #include "TestTU.h"
 #include "clang/Basic/SourceLocation.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
+#include 
 
 namespace clang {
 namespace clangd {
@@ -21,19 +23,24 @@
 
 using testing::UnorderedElementsAreArray;
 
+MATCHER_P(rangeIs, R, "") { return arg.Rng == R; }
+MATCHER(isDef, "") { return arg.IsDefinition; }
+MATCHER(inConditionalDirective, "") { return arg.InConditionalDirective; }
+
 TEST(CollectMainFileMacros, SelectedMacros) {
   // References of the same symbol must have the ranges with the same
   // name(integer). If there are N different symbols then they must be named
   // from 1 to N. Macros for which SymbolID cannot be computed must be named
-  // "Unknown".
+  // "Unknown". The payload of the annotation describes the extra bit
+  // information of the MacroOccurrence (e.g. $1(def) => IsDefinition).
   const char *Tests[] = {
   R"cpp(// Macros: Cursor on definition.
-#define $1[[FOO]](x,y) (x + y)
+#define $1(def)[[FOO]](x,y) (x + y)
 int main() { int x = $1[[FOO]]($1[[FOO]](3, 4), $1[[FOO]](5, 6)); }
   )cpp",
   R"cpp(
-#define $1[[M]](X) X;
-#define $2[[abc]] 123
+#define $1(def)[[M]](X) X;
+#define $2(def)[[abc]] 123
 int s = $1[[M]]($2[[abc]]);
   )cpp",
   // FIXME: Locating macro in duplicate definitions doesn't work. Enable
@@ -48,31 +55,50 @@
   //   #undef $2[[abc]]
   // )cpp",
   R"cpp(
-#ifdef $Unknown[[UNDEFINED]]
+#ifdef $Unknown(condit)[[UNDEFINED]]
+#endif
+
+#ifndef $Unknown(condit)[[UNDEFINED]]
+#endif
+
+#if defined($Unknown(condit)[[UNDEFINED]])
 #endif
   )cpp",
   R"cpp(
-#ifndef $Unknown[[abc]]
-#define $1[[abc]]
-#ifdef $1[[abc]]
+#ifndef $Unknown(condit)[[abc]]
+#define $1(def)[[abc]]
+#ifdef $1(condit)[[abc]]
 #endif
 #endif
   )cpp",
   R"cpp(
 // Macros from token concatenations not included.
-#define $1[[CONCAT]](X) X##A()
-#define $2[[PREPEND]](X) MACRO##X()
-#define $3[[MACROA]]() 123
+#define $1(def)[[CONCAT]](X) X##A()
+#define $2(def)[[PREPEND]](X) MACRO##X()
+#define $3(def)[[MACROA]]() 123
 int B = $1[[CONCAT]](MACRO);
 int D = $2[[PREPEND]](A);
   )cpp",
   R"cpp(
-// FIXME: Macro names in a definition are not detected.
-#define $1[[MACRO_ARGS2]](X, Y) X Y
-#define $2[[FOO]] BAR
-#define $3[[BAR]] 1
+#define $1(def)[[MACRO_ARGS2]](X, Y) X Y
+#define $3(def)[[BAR]] 1
+#define $2(def)[[FOO]] $3[[BAR]]
 int A = $2[[FOO]];
   )cpp"};
+  auto ExpectedResults = [](const Annotations , StringRef Name) {
+std::vector> ExpectedLocations;
+for (const auto &[R, Bits] : 

[PATCH] D146279: [clangd] Extend CollectMainFileMacros.

2023-03-20 Thread Kadir Cetinkaya via Phabricator via cfe-commits
kadircet accepted this revision.
kadircet added a comment.
This revision is now accepted and ready to land.

thanks!




Comment at: clang-tools-extra/clangd/CollectMacros.h:48
 public:
-  explicit CollectMainFileMacros(const SourceManager , MainFileMacros )
-  : SM(SM), Out(Out) {}
+  explicit CollectMainFileMacros(const SourceManager ,
+ const Preprocessor , MainFileMacros )

nit: you can get SM out of PP, no need to pass both.



Comment at: clang-tools-extra/clangd/CollectMacros.h:70
 
   void Ifdef(SourceLocation Loc, const Token ,
  const MacroDefinition ) override {

nit: can you actually move macro bodies out-of-line ? it's unfortunate that we 
need to build half of the project everytime we touch this header.



Comment at: clang-tools-extra/clangd/ParsedAST.cpp:615
+  std::make_unique(
+  Clang->getSourceManager(), Clang->getPreprocessor(), Macros));
 

nit: we seem to be calling `Clang->getPreprocessor()` in many places, maybe 
extract it into a variable instead?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146279/new/

https://reviews.llvm.org/D146279

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D146279: [clangd] Extend CollectMainFileMacros.

2023-03-17 Thread Haojian Wu via Phabricator via cfe-commits
hokein created this revision.
hokein added a reviewer: kadircet.
Herald added a subscriber: arphaman.
Herald added a project: All.
hokein requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Extend the existing MainFileMacros structure:

- record more information (InConditionalDirective) in MacroOccurrence
- collect macro references inside macro body (fix a long-time FIXME)

So that the MainFileMacros preseve enough information, which allows a
just-in-time convertion to interop with include-cleaner::Macro for
include-cleaer features.

See the context in https://reviews.llvm.org/D146017.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146279

Files:
  clang-tools-extra/clangd/CollectMacros.cpp
  clang-tools-extra/clangd/CollectMacros.h
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/Preamble.cpp
  clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
  clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -399,7 +399,7 @@
   #define $Macro_decl[[MACRO_CONCAT]](X, V, T) T foo##X = V
   #define $Macro_decl[[DEF_VAR]](X, V) int X = V
   #define $Macro_decl[[DEF_VAR_T]](T, X, V) T X = V
-  #define $Macro_decl[[DEF_VAR_REV]](V, X) DEF_VAR(X, V)
+  #define $Macro_decl[[DEF_VAR_REV]](V, X) $Macro[[DEF_VAR]](X, V)
   #define $Macro_decl[[CPY]](X) X
   #define $Macro_decl[[DEF_VAR_TYPE]](X, Y) X Y
   #define $Macro_decl[[SOME_NAME]] variable
@@ -431,7 +431,7 @@
 )cpp",
   R"cpp(
   #define $Macro_decl[[fail]](expr) expr
-  #define $Macro_decl[[assert]](COND) if (!(COND)) { fail("assertion failed" #COND); }
+  #define $Macro_decl[[assert]](COND) if (!(COND)) { $Macro[[fail]]("assertion failed" #COND); }
   // Preamble ends.
   int $Variable_def[[x]];
   int $Variable_def[[y]];
Index: clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
===
--- clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
+++ clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp
@@ -8,12 +8,14 @@
 #include "AST.h"
 #include "Annotations.h"
 #include "CollectMacros.h"
+#include "Matchers.h"
 #include "SourceCode.h"
 #include "TestTU.h"
 #include "clang/Basic/SourceLocation.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
+#include 
 
 namespace clang {
 namespace clangd {
@@ -21,19 +23,24 @@
 
 using testing::UnorderedElementsAreArray;
 
+MATCHER_P(rangeIs, R, "") { return arg.Rng == R; }
+MATCHER(isDef, "") { return arg.IsDefinition; }
+MATCHER(inConditionalDirective, "") { return arg.InConditionalDirective; }
+
 TEST(CollectMainFileMacros, SelectedMacros) {
   // References of the same symbol must have the ranges with the same
   // name(integer). If there are N different symbols then they must be named
   // from 1 to N. Macros for which SymbolID cannot be computed must be named
-  // "Unknown".
+  // "Unknown". The payload of the annotation describes the extra bit
+  // information of the MacroOccurrence (e.g. $1(def) => IsDefinition).
   const char *Tests[] = {
   R"cpp(// Macros: Cursor on definition.
-#define $1[[FOO]](x,y) (x + y)
+#define $1(def)[[FOO]](x,y) (x + y)
 int main() { int x = $1[[FOO]]($1[[FOO]](3, 4), $1[[FOO]](5, 6)); }
   )cpp",
   R"cpp(
-#define $1[[M]](X) X;
-#define $2[[abc]] 123
+#define $1(def)[[M]](X) X;
+#define $2(def)[[abc]] 123
 int s = $1[[M]]($2[[abc]]);
   )cpp",
   // FIXME: Locating macro in duplicate definitions doesn't work. Enable
@@ -48,31 +55,50 @@
   //   #undef $2[[abc]]
   // )cpp",
   R"cpp(
-#ifdef $Unknown[[UNDEFINED]]
+#ifdef $Unknown(condit)[[UNDEFINED]]
+#endif
+
+#ifndef $Unknown(condit)[[UNDEFINED]]
+#endif
+
+#if defined($Unknown(condit)[[UNDEFINED]])
 #endif
   )cpp",
   R"cpp(
-#ifndef $Unknown[[abc]]
-#define $1[[abc]]
-#ifdef $1[[abc]]
+#ifndef $Unknown(condit)[[abc]]
+#define $1(def)[[abc]]
+#ifdef $1(condit)[[abc]]
 #endif
 #endif
   )cpp",
   R"cpp(
 // Macros from token concatenations not included.
-#define $1[[CONCAT]](X) X##A()
-#define $2[[PREPEND]](X) MACRO##X()
-#define $3[[MACROA]]() 123
+#define $1(def)[[CONCAT]](X) X##A()
+#define $2(def)[[PREPEND]](X) MACRO##X()
+#define $3(def)[[MACROA]]() 123
 int B = $1[[CONCAT]](MACRO);
 int D = $2[[PREPEND]](A);
   )cpp",
   R"cpp(
-