[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
jvikstrom marked an inline comment as done. jvikstrom added inline comments. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:75 + SemanticSymbolASTCollector Collector(Ctx); + Collector.TraverseAST(Ctx); + return Collector.getSymbols(); jvikstrom wrote: > hokein wrote: > > let's move the above lines into `SemanticSymbolASTCollector`, we can define > > a new method "collectTokens()". > Should I expose the entire class or keep the getSemanticHighlights function? > (I'm just thinking that RecursiveASTVisitor contains a lot of public > functions which makes it not super obvious which function to call to get > semantic highlight things when looking at autocomplete) Actually I can just do private inheritance and declare RecursiveASTVisitor to be a friend. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63559/new/ https://reviews.llvm.org/D63559 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
jvikstrom added inline comments. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:75 + SemanticSymbolASTCollector Collector(Ctx); + Collector.TraverseAST(Ctx); + return Collector.getSymbols(); hokein wrote: > let's move the above lines into `SemanticSymbolASTCollector`, we can define a > new method "collectTokens()". Should I expose the entire class or keep the getSemanticHighlights function? (I'm just thinking that RecursiveASTVisitor contains a lot of public functions which makes it not super obvious which function to call to get semantic highlight things when looking at autocomplete) Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63559/new/ https://reviews.llvm.org/D63559 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
jvikstrom updated this revision to Diff 206247. jvikstrom marked 7 inline comments as done. jvikstrom added a comment. Fixed tests and edge case with function declarations without names for parameters. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63559/new/ https://reviews.llvm.org/D63559 Files: clang-tools-extra/clangd/CMakeLists.txt clang-tools-extra/clangd/SemanticHighlight.cpp clang-tools-extra/clangd/SemanticHighlight.h clang-tools-extra/clangd/unittests/CMakeLists.txt clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp Index: clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp === --- /dev/null +++ clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp @@ -0,0 +1,63 @@ +//===- SemanticHighlightTest.cpp - SemanticHighlightTest tests-*- C++ -* -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "Annotations.h" +#include "ClangdUnit.h" +#include "Protocol.h" +#include "SemanticHighlight.h" +#include "SourceCode.h" +#include "TestTU.h" +#include "llvm/Support/ScopedPrinter.h" +#include "gmock/gmock-matchers.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace clang { +namespace clangd { +namespace { + +void checkTokensExists(std::vector Tokens, + std::vector ExpectedRanges, + SemanticHighlightKind Kind) { + std::vector ActualRanges; + for (SemanticToken Token : Tokens) { +if (Token.Kind == Kind) { + ActualRanges.push_back(Token.R); +} + } + + EXPECT_THAT(ActualRanges, testing::UnorderedElementsAreArray(ExpectedRanges)); +} + +TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) { + std::string Preamble = R"cpp( +void $Function[[foo]](int); +struct A { + double SomeMember; +}; +void $Function[[foo]](int $Variable[[a]]) { + SOMEDECL( ); + auto $Variable[[VeryLongVariableName]] = 12312; + A $Variable[[aa]]; +} + )cpp"; + + Annotations Test(Preamble); + auto Variables = Test.ranges("Variable"); + auto Function = Test.ranges("Function"); + + auto AST = TestTU::withCode(Test.code()).build(); + auto Tokens = getSemanticHighlights(AST); + + checkTokensExists(Tokens, Variables, SemanticHighlightKind::Variable); + checkTokensExists(Tokens, Function, SemanticHighlightKind::Function); +} + +} // namespace +} // namespace clangd +} // namespace clang Index: clang-tools-extra/clangd/unittests/CMakeLists.txt === --- clang-tools-extra/clangd/unittests/CMakeLists.txt +++ clang-tools-extra/clangd/unittests/CMakeLists.txt @@ -53,6 +53,7 @@ RenameTests.cpp RIFFTests.cpp SelectionTests.cpp + SemanticHighlightTests.cpp SerializationTests.cpp SourceCodeTests.cpp SymbolCollectorTests.cpp Index: clang-tools-extra/clangd/SemanticHighlight.h === --- /dev/null +++ clang-tools-extra/clangd/SemanticHighlight.h @@ -0,0 +1,37 @@ +//==-- SemanticHighlight.h - Generating highlights from the AST--*- C++ -*-==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHT_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHT_H + +#include "ClangdUnit.h" +#include "Protocol.h" + +namespace clang { +namespace clangd { + +enum class SemanticHighlightKind : int { + Variable = 0, + Function = 1, +}; + +// Contains all information needed for the highlighting a token. +struct SemanticToken { + SemanticHighlightKind Kind; + Range R; +}; + +bool operator==(const SemanticToken , const SemanticToken ); +bool operator!=(const SemanticToken , const SemanticToken ); + +// Returns semantic highlights for the AST. +std::vector getSemanticHighlights(ParsedAST ); + +} // namespace clangd +} // namespace clang + +#endif Index: clang-tools-extra/clangd/SemanticHighlight.cpp === --- /dev/null +++ clang-tools-extra/clangd/SemanticHighlight.cpp @@ -0,0 +1,82 @@ +#include "SemanticHighlight.h" +#include "SourceCode.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Lex/Lexer.h" + +namespace clang { +namespace clangd { +namespace { + +// Collects all semantic tokens in an ASTContext. +class SemanticSymbolASTCollector +: public RecursiveASTVisitor { + std::vector
[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
Eugene.Zelenko added inline comments. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:1 +#include "SemanticHighlight.h" +#include "SourceCode.h" License header was not added despite comment was marked as done. Comment at: clang-tools-extra/clangd/SemanticHighlight.h:13 +//===--===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHT_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHT_H Separator line was not added despite comment was marked as done. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63559/new/ https://reviews.llvm.org/D63559 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
hokein added inline comments. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:11 + +// Collects all semantic symbols in an ASTContext. Symbols on line i are always +// in front of symbols on line i+1 The comment is out of date now. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:39 +AST.getLangOpts()); +if (Len == 0) { + // Don't add symbols that don't have any length. We can check the name of `NamedDecl` is empty rather than detecting the length. Could you add a test for this? Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:75 + SemanticSymbolASTCollector Collector(Ctx); + Collector.TraverseAST(Ctx); + return Collector.getSymbols(); let's move the above lines into `SemanticSymbolASTCollector`, we can define a new method "collectTokens()". Comment at: clang-tools-extra/clangd/SemanticHighlight.h:9 +// +// Code for collecting semantic symbols from the C++ AST using the +// RecursiveASTVisitor this comment doesn't provide much information IMO, let's remove it. Comment at: clang-tools-extra/clangd/SemanticHighlight.h:29 +struct SemanticToken { + SemanticToken() = default; + SemanticToken(SemanticHighlightKind Kind, Range R) : Kind(Kind), R(R) {} We can get rid of the default constructor and the constructor below. Comment at: clang-tools-extra/clangd/SemanticHighlight.h:38 + +// Returns semantic highlights for the AST. The vector is ordered in ascending +// order by the line number. Every symbol in LineHighlight is ordered in The comment is out of date. Comment at: clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp:31 +void $foo[[foo]](int $a[[a]]) { + auto $vlvn[[VeryLongVariableName]] = 12312; + A $aa[[aa]]; Thinking more about the test, I think we could improve the test to make it more scalable and less verbose code. How about associating the range name (e.g. $vlan) in the annotation with the name in `SemanticHighlightKind`? For example, in this test case, we could use annotation like ``` void $Function[foo](int $Variable[[a]]) { auto $Variable[[A]] = 222; } ``` We have a common function like `checkHighlights` which verifies that all `Functions` and `Variable` ranges. ``` void checkHighlights(... annotation) { auto ActualHighlights = getHighlights(annotation.code()); EXPECT_THAT(annotations.ranges('Function'), unorderedElemenetsAre(filter(ActualHighlights, SemanticHighlightKind::Function)); EXPECT_THAT(annotations.ranges('Variable'), unorderedElemenetsAre(filter(ActualHighlights, SemanticHighlightKind::Variable)) } ``` Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63559/new/ https://reviews.llvm.org/D63559 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
jvikstrom marked 2 inline comments as done. jvikstrom added inline comments. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:129 +std::vector> getSemanticScopes() { + return {{"variable"}, {"entity.name.function"}}; +} hokein wrote: > This is Textmate-specific, I think we should lift it to a new File > (TextMate.cpp). We can do it in a separate patch. > > I'd use a map to explicitly express the relationship between `SemanticScope` > and TextMate scope, the current implementation is not obvious. > > ``` > {SemanticScope::Function, "entity.name.function"}, > {SemanticScope::Variable, "variable.other"}, > ``` Will do this in CL when adding C++ ClangdServer api calls Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63559/new/ https://reviews.llvm.org/D63559 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
jvikstrom updated this revision to Diff 206178. jvikstrom marked 18 inline comments as done. jvikstrom added a comment. Removed LSP specific stuff. Changed a bunch of types and names. Added fixme for macro expansion. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63559/new/ https://reviews.llvm.org/D63559 Files: clang-tools-extra/clangd/CMakeLists.txt clang-tools-extra/clangd/SemanticHighlight.cpp clang-tools-extra/clangd/SemanticHighlight.h clang-tools-extra/clangd/unittests/CMakeLists.txt clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp Index: clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp === --- /dev/null +++ clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp @@ -0,0 +1,55 @@ +//===- SemanticHighlightTest.cpp - SemanticHighlightTest tests-*- C++ -* -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "Annotations.h" +#include "ClangdUnit.h" +#include "Protocol.h" +#include "SemanticHighlight.h" +#include "SourceCode.h" +#include "TestTU.h" +#include "llvm/Support/ScopedPrinter.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace clang { +namespace clangd { +namespace { + +using ::testing::ElementsAreArray; + +TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) { + std::string Preamble = R"cpp( +struct A { + double SomeMember; +}; +void $foo[[foo]](int $a[[a]]) { + auto $vlvn[[VeryLongVariableName]] = 12312; + A $aa[[aa]]; +} + )cpp"; + + Annotations Test(Preamble); + auto Foo = Test.range("foo"); + auto A = Test.range("a"); + auto VeryLong = Test.range("vlvn"); + auto AA = Test.range("aa"); + std::vector CorrectTokens = std::vector{ + SemanticToken(SemanticHighlightKind::Function, Foo), + SemanticToken(SemanticHighlightKind::Variable, A), + SemanticToken(SemanticHighlightKind::Variable, VeryLong), + SemanticToken(SemanticHighlightKind::Variable, AA)}; + + auto AST = TestTU::withCode(Test.code()).build(); + auto Tokens = getSemanticHighlights(AST); + + EXPECT_THAT(Tokens, ElementsAreArray(CorrectTokens)); +} + +} // namespace +} // namespace clangd +} // namespace clang Index: clang-tools-extra/clangd/unittests/CMakeLists.txt === --- clang-tools-extra/clangd/unittests/CMakeLists.txt +++ clang-tools-extra/clangd/unittests/CMakeLists.txt @@ -53,6 +53,7 @@ RenameTests.cpp RIFFTests.cpp SelectionTests.cpp + SemanticHighlightTests.cpp SerializationTests.cpp SourceCodeTests.cpp SymbolCollectorTests.cpp Index: clang-tools-extra/clangd/SemanticHighlight.h === --- /dev/null +++ clang-tools-extra/clangd/SemanticHighlight.h @@ -0,0 +1,46 @@ +//==-- SemanticHighlight.h - Generating highlights from the AST--*- C++ -*-==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// Code for collecting semantic symbols from the C++ AST using the +// RecursiveASTVisitor +// +//===--===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHT_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHT_H + +#include "ClangdUnit.h" +#include "Protocol.h" + +namespace clang { +namespace clangd { + +enum class SemanticHighlightKind : int { + Variable = 0, + Function = 1, +}; + +// Contains all information needed for the highlighting a symbol. +struct SemanticToken { + SemanticToken() = default; + SemanticToken(SemanticHighlightKind Kind, Range R) : Kind(Kind), R(R) {} + SemanticHighlightKind Kind; + Range R; +}; + +bool operator==(const SemanticToken , const SemanticToken ); +bool operator!=(const SemanticToken , const SemanticToken ); + +// Returns semantic highlights for the AST. The vector is ordered in ascending +// order by the line number. Every symbol in LineHighlight is ordered in +// ascending order by their coumn number. +std::vector getSemanticHighlights(ParsedAST ); + +} // namespace clangd +} // namespace clang + +#endif Index: clang-tools-extra/clangd/SemanticHighlight.cpp === --- /dev/null +++ clang-tools-extra/clangd/SemanticHighlight.cpp @@ -0,0 +1,79 @@ +#include "SemanticHighlight.h" +#include "SourceCode.h" +#include "clang/AST/ASTContext.h" +#include
[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
hokein added a comment. Thanks, I think we can simplify the interface further, see my comments inline. The boundary of this patch seems unclear, currently it contains C++ APIs, and some implementations for semantic highlighting LSP. I think we should merely focus on the C++ APIs in this patch. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:32 + void addSymbol(Decl *D, SemanticScope Scope) { +auto Loc = D->getLocation(); +SemanticToken S; Looks like the code doesn't handle the case where D is expanded from a macro (Loc is a macro location). Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:129 +std::vector> getSemanticScopes() { + return {{"variable"}, {"entity.name.function"}}; +} This is Textmate-specific, I think we should lift it to a new File (TextMate.cpp). We can do it in a separate patch. I'd use a map to explicitly express the relationship between `SemanticScope` and TextMate scope, the current implementation is not obvious. ``` {SemanticScope::Function, "entity.name.function"}, {SemanticScope::Variable, "variable.other"}, ``` Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:133 +std::vector getASTHighlights(ASTContext ) { + SemanticSymbolASTCollector Collector(AST); + Collector.TraverseAST(AST); We should only collect the highlights from the main AST, I think we should set traversal scope only to `localTopDecls` (use `ParsedAST` as parameter would help here) `ASTCtx.setTraversalScope(MainAST.getLocalTopLevelDecls());` Comment at: clang-tools-extra/clangd/SemanticHighlight.h:14 +//===--===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H nit: the name of header guard doesn't reflect the file name. Comment at: clang-tools-extra/clangd/SemanticHighlight.h:21 +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Lex/Lexer.h" + please clean-up your #includes: - Headers.h is unused - we use RecursiveASTVisitor, Lexer in the .cpp file, we should include these headers in the .cpp file rather the `.h` file. Comment at: clang-tools-extra/clangd/SemanticHighlight.h:26 + +// ScopeIndex represents the mapping from the scopes list to a type of +// expression. The comment seems not very related to this structure. We'd use this `enum` class to find a corresponding `TextMate` scope in the future, but this is implementation detail, the comment here should mention the high-level things about this structure. Comment at: clang-tools-extra/clangd/SemanticHighlight.h:28 +// expression. +enum class SemanticScope : int { + VariableDeclaration = 0, TextMate is using the term `scope` for different tokens, but the scope has different meaning in C++ world (usually the namespace "ns::" of a symbol). I'd avoid using this word in the C++ interface. Just `SemanticHighlightingKind`? Comment at: clang-tools-extra/clangd/SemanticHighlight.h:29 +enum class SemanticScope : int { + VariableDeclaration = 0, + FunctionDeclaration = 1, not exactly sure whether we should distinguish these cases at the moment (function declaration vs function calls, variable declaration vs variable references). I think we could just use `Variable`, `Function` at the beginning, and add more kinds afterwards. Comment at: clang-tools-extra/clangd/SemanticHighlight.h:39 + SemanticScope Scope; + Position StartPosition; + unsigned int Len; instead of using `start position + length`, I'd use `Range R;` Comment at: clang-tools-extra/clangd/SemanticHighlight.h:61 +// ascending order by their coumn number. +std::vector getASTHighlights(ASTContext ); +std::vector> getSemanticScopes(); I'd drop the `AST` word, how about naming it "getSemanticHighlights"? I think we can just return "vector", and we could we could group them by line when returning a LSP result. The function parameter should be `ParsedAST`. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63559/new/ https://reviews.llvm.org/D63559 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
Eugene.Zelenko added inline comments. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:1 +#include "SemanticHighlight.h" + Header is missing. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:32 + void addSymbol(Decl *D, SemanticScope Scope) { +auto Loc = D->getLocation(); +SemanticSymbol S; Please don't use auto if type is not spelled explicitly. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:34 +SemanticSymbol S; +auto LSPLoc = sourceLocToPosition(SM, Loc); + Please don't use auto if type is not spelled explicitly. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:99 +void write32be(uint32_t I, llvm::raw_ostream ) { + char Buf[4]; + llvm::support::endian::write32be(Buf, I); May be std::array is better way to do this? Same below. Comment at: clang-tools-extra/clangd/SemanticHighlight.cpp:135 + Collector.TraverseAST(AST); + auto Symbols = Collector.getSymbols(); + std::vector Lines; Please don't use auto if type is not spelled explicitly. Comment at: clang-tools-extra/clangd/SemanticHighlight.h:2 +//===--- SemanticHighlight.h - Generating highlights from the AST +//-*- C++ -*-===// +// Please merge with previous line. If it doesn't fit, remove some - and =. Comment at: clang-tools-extra/clangd/SemanticHighlight.h:14 +//===--===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H Please separate with empty line. Comment at: clang-tools-extra/clangd/SemanticHighlight.h:35 +struct SemanticSymbol { + SemanticSymbol() {} + SemanticSymbol(SemanticScope Scope, Position StartPosition, unsigned int Len) Please use = default; Comment at: clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp:2 +//===-- SemanticSymbolASTCollectorTests.cpp - SemanticSymbolASTCollector tests +//--*- C++ -*-===// +// Please merge with previous line. If it doesn't fit, remove some - and =. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63559/new/ https://reviews.llvm.org/D63559 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
jvikstrom updated this revision to Diff 205626. jvikstrom added a comment. Renamed SemanticSymbol to SemanticToken Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63559/new/ https://reviews.llvm.org/D63559 Files: clang-tools-extra/clangd/CMakeLists.txt clang-tools-extra/clangd/SemanticHighlight.cpp clang-tools-extra/clangd/SemanticHighlight.h clang-tools-extra/clangd/unittests/CMakeLists.txt clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp Index: clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp === --- /dev/null +++ clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp @@ -0,0 +1,76 @@ +//===-- SemanticSymbolASTCollectorTests.cpp - SemanticSymbolASTCollector tests +//--*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "Annotations.h" +#include "ClangdUnit.h" +#include "Protocol.h" +#include "SemanticHighlight.h" +#include "SourceCode.h" +#include "TestTU.h" +#include "llvm/Support/ScopedPrinter.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace clang { +namespace clangd { +namespace { + +using ::testing::ElementsAreArray; + +Position createPosition(int Line, int Character) { + Position Pos; + Pos.character = Character; + Pos.line = Line; + return Pos; +} + +TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) { + std::string Preamble = R"cpp( +struct A { + double SomeMember; +}; +void $foo[[foo]](int $a[[a]]) { + auto $vlvn[[VeryLongVariableName]] = 12312; + A $aa[[aa]]; +} + )cpp"; + + Annotations Test(Preamble); + auto Foo = Test.range("foo"); + auto A = Test.range("a"); + auto VeryLong = Test.range("vlvn"); + auto AA = Test.range("aa"); + std::vector CorrectLines = std::vector{ + LineHighlight( + Foo.start.line, + {SemanticToken(SemanticScope::FunctionDeclaration, + createPosition(Foo.start.line, Foo.start.character), + 3), + SemanticToken(SemanticScope::VariableDeclaration, + createPosition(A.start.line, A.start.character), 1)}), + LineHighlight( + VeryLong.start.line, + {SemanticToken( + SemanticScope::VariableDeclaration, + createPosition(VeryLong.start.line, VeryLong.start.character), + VeryLong.end.character - VeryLong.start.character)}), + LineHighlight( + AA.start.line, + {SemanticToken(SemanticScope::VariableDeclaration, + createPosition(AA.start.line, AA.start.character), + 2)})}; + + auto AST = TestTU::withCode(Test.code()).build(); + auto Lines = getASTHighlights(AST.getASTContext()); + EXPECT_THAT(Lines, ElementsAreArray(CorrectLines)); +} + +} // namespace +} // namespace clangd +} // namespace clang Index: clang-tools-extra/clangd/unittests/CMakeLists.txt === --- clang-tools-extra/clangd/unittests/CMakeLists.txt +++ clang-tools-extra/clangd/unittests/CMakeLists.txt @@ -53,6 +53,7 @@ RenameTests.cpp RIFFTests.cpp SelectionTests.cpp + SemanticHighlightTests.cpp SerializationTests.cpp SourceCodeTests.cpp SymbolCollectorTests.cpp Index: clang-tools-extra/clangd/SemanticHighlight.h === --- /dev/null +++ clang-tools-extra/clangd/SemanticHighlight.h @@ -0,0 +1,67 @@ +//===--- SemanticHighlight.h - Generating highlights from the AST +//-*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// Code for collecting semantic symbols from the C++ AST using the +// RecursiveASTVisitor +// +//===--===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H + +#include "AST.h" +#include "Headers.h" +#include "Protocol.h" +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Lex/Lexer.h" + +namespace clang { +namespace clangd { + +// ScopeIndex represents the mapping from the scopes list to a type of +// expression. +enum class SemanticScope : int { + VariableDeclaration = 0, + FunctionDeclaration = 1, +}; + +// Contains all information needed for the highlighting a symbol. +struct SemanticToken {
[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
jvikstrom updated this revision to Diff 205608. jvikstrom added a comment. Herald added a subscriber: mgorny. Adds CMakeLists changes Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63559/new/ https://reviews.llvm.org/D63559 Files: clang-tools-extra/clangd/CMakeLists.txt clang-tools-extra/clangd/SemanticHighlight.cpp clang-tools-extra/clangd/SemanticHighlight.h clang-tools-extra/clangd/unittests/CMakeLists.txt clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp Index: clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp === --- /dev/null +++ clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp @@ -0,0 +1,76 @@ +//===-- SemanticSymbolASTCollectorTests.cpp - SemanticSymbolASTCollector tests +//--*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "Annotations.h" +#include "ClangdUnit.h" +#include "Protocol.h" +#include "SemanticHighlight.h" +#include "SourceCode.h" +#include "TestTU.h" +#include "llvm/Support/ScopedPrinter.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace clang { +namespace clangd { +namespace { + +using ::testing::ElementsAreArray; + +Position createPosition(int Line, int Character) { + Position Pos; + Pos.character = Character; + Pos.line = Line; + return Pos; +} + +TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) { + std::string Preamble = R"cpp( +struct A { + double SomeMember; +}; +void $foo[[foo]](int $a[[a]]) { + auto $vlvn[[VeryLongVariableName]] = 12312; + A $aa[[aa]]; +} + )cpp"; + + Annotations Test(Preamble); + auto Foo = Test.range("foo"); + auto A = Test.range("a"); + auto VeryLong = Test.range("vlvn"); + auto AA = Test.range("aa"); + std::vector CorrectLines = std::vector{ + LineHighlight( + Foo.start.line, + {SemanticSymbol(SemanticScope::FunctionDeclaration, + createPosition(Foo.start.line, Foo.start.character), + 3), + SemanticSymbol(SemanticScope::VariableDeclaration, + createPosition(A.start.line, A.start.character), 1)}), + LineHighlight( + VeryLong.start.line, + {SemanticSymbol( + SemanticScope::VariableDeclaration, + createPosition(VeryLong.start.line, VeryLong.start.character), + VeryLong.end.character - VeryLong.start.character)}), + LineHighlight( + AA.start.line, + {SemanticSymbol(SemanticScope::VariableDeclaration, + createPosition(AA.start.line, AA.start.character), + 2)})}; + + auto AST = TestTU::withCode(Test.code()).build(); + auto Lines = getASTHighlights(AST.getASTContext()); + EXPECT_THAT(Lines, ElementsAreArray(CorrectLines)); +} + +} // namespace +} // namespace clangd +} // namespace clang Index: clang-tools-extra/clangd/unittests/CMakeLists.txt === --- clang-tools-extra/clangd/unittests/CMakeLists.txt +++ clang-tools-extra/clangd/unittests/CMakeLists.txt @@ -53,6 +53,7 @@ RenameTests.cpp RIFFTests.cpp SelectionTests.cpp + SemanticHighlightTests.cpp SerializationTests.cpp SourceCodeTests.cpp SymbolCollectorTests.cpp Index: clang-tools-extra/clangd/SemanticHighlight.h === --- /dev/null +++ clang-tools-extra/clangd/SemanticHighlight.h @@ -0,0 +1,67 @@ +//===--- SemanticHighlight.h - Generating highlights from the AST +//-*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// Code for collecting semantic symbols from the C++ AST using the +// RecursiveASTVisitor +// +//===--===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H + +#include "AST.h" +#include "Headers.h" +#include "Protocol.h" +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Lex/Lexer.h" + +namespace clang { +namespace clangd { + +// ScopeIndex represents the mapping from the scopes list to a type of +// expression. +enum class SemanticScope : int { + VariableDeclaration = 0, + FunctionDeclaration = 1, +}; + +// Contains all information needed for the highlighting a symbol.
[PATCH] D63559: [clang-tidy] Added functionality for getting semantic highlights for variable and function declarations
jvikstrom created this revision. jvikstrom added reviewers: hokein, ilya-biryukov. Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, xazax.hun. Herald added a project: clang. Adds functionality for getting semantic highlights Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D63559 Files: clang-tools-extra/clangd/SemanticHighlight.cpp clang-tools-extra/clangd/SemanticHighlight.h clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp Index: clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp === --- /dev/null +++ clang-tools-extra/clangd/unittests/SemanticHighlightTests.cpp @@ -0,0 +1,76 @@ +//===-- SemanticSymbolASTCollectorTests.cpp - SemanticSymbolASTCollector tests +//--*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "Annotations.h" +#include "ClangdUnit.h" +#include "Protocol.h" +#include "SemanticHighlight.h" +#include "SourceCode.h" +#include "TestTU.h" +#include "llvm/Support/ScopedPrinter.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace clang { +namespace clangd { +namespace { + +using ::testing::ElementsAreArray; + +Position createPosition(int Line, int Character) { + Position Pos; + Pos.character = Character; + Pos.line = Line; + return Pos; +} + +TEST(SemanticSymbolASTCollector, GetBeginningOfIdentifier) { + std::string Preamble = R"cpp( +struct A { + double SomeMember; +}; +void $foo[[foo]](int $a[[a]]) { + auto $vlvn[[VeryLongVariableName]] = 12312; + A $aa[[aa]]; +} + )cpp"; + + Annotations Test(Preamble); + auto Foo = Test.range("foo"); + auto A = Test.range("a"); + auto VeryLong = Test.range("vlvn"); + auto AA = Test.range("aa"); + std::vector CorrectLines = std::vector{ + LineHighlight( + Foo.start.line, + {SemanticSymbol(SemanticScope::FunctionDeclaration, + createPosition(Foo.start.line, Foo.start.character), + 3), + SemanticSymbol(SemanticScope::VariableDeclaration, + createPosition(A.start.line, A.start.character), 1)}), + LineHighlight( + VeryLong.start.line, + {SemanticSymbol( + SemanticScope::VariableDeclaration, + createPosition(VeryLong.start.line, VeryLong.start.character), + VeryLong.end.character - VeryLong.start.character)}), + LineHighlight( + AA.start.line, + {SemanticSymbol(SemanticScope::VariableDeclaration, + createPosition(AA.start.line, AA.start.character), + 2)})}; + + auto AST = TestTU::withCode(Test.code()).build(); + auto Lines = getASTHighlights(AST.getASTContext()); + EXPECT_THAT(Lines, ElementsAreArray(CorrectLines)); +} + +} // namespace +} // namespace clangd +} // namespace clang Index: clang-tools-extra/clangd/SemanticHighlight.h === --- /dev/null +++ clang-tools-extra/clangd/SemanticHighlight.h @@ -0,0 +1,67 @@ +//===--- SemanticHighlight.h - Generating highlights from the AST +//-*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// Code for collecting semantic symbols from the C++ AST using the +// RecursiveASTVisitor +// +//===--===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICSYMBOLASTCOLLECTOR_H + +#include "AST.h" +#include "Headers.h" +#include "Protocol.h" +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Lex/Lexer.h" + +namespace clang { +namespace clangd { + +// ScopeIndex represents the mapping from the scopes list to a type of +// expression. +enum class SemanticScope : int { + VariableDeclaration = 0, + FunctionDeclaration = 1, +}; + +// Contains all information needed for the highlighting a symbol. +struct SemanticSymbol { + SemanticSymbol() {} + SemanticSymbol(SemanticScope Scope, Position StartPosition, unsigned int Len) + : Scope(Scope), StartPosition(StartPosition), Len(Len) {} + SemanticScope Scope; + Position StartPosition; + unsigned int Len; +}; + +bool operator==(const SemanticSymbol , const SemanticSymbol ); +bool operator!=(const SemanticSymbol , const SemanticSymbol ); + +// Contains all highlights in a single line. +struct