[clang-tools-extra] r370482 - [clangd] Add highlighting for macro expansions.
Author: jvikstrom Date: Fri Aug 30 08:47:27 2019 New Revision: 370482 URL: http://llvm.org/viewvc/llvm-project?rev=370482=rev Log: [clangd] Add highlighting for macro expansions. Summary: https://github.com/clangd/clangd/issues/134 Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66995 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/SemanticHighlighting.h clang-tools-extra/trunk/clangd/test/semantic-highlighting.test clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=370482=370481=370482=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Fri Aug 30 08:47:27 2019 @@ -24,16 +24,20 @@ namespace { class HighlightingTokenCollector : public RecursiveASTVisitor { std::vector Tokens; - ASTContext - const SourceManager + ParsedAST public: - HighlightingTokenCollector(ParsedAST ) - : Ctx(AST.getASTContext()), SM(AST.getSourceManager()) {} + HighlightingTokenCollector(ParsedAST ) : AST(AST) {} std::vector collectTokens() { Tokens.clear(); -TraverseAST(Ctx); +TraverseAST(AST.getASTContext()); +// Add highlightings for macro expansions as they are not traversed by the +// visitor. +// FIXME: Should add highlighting to the macro definitions as well. But this +// information is not collected in ParsedAST right now. +for (const SourceLocation : AST.getMainFileExpansions()) + addToken(L, HighlightingKind::Macro); // Initializer lists can give duplicates of tokens, therefore all tokens // must be deduplicated. llvm::sort(Tokens); @@ -264,6 +268,7 @@ private: } void addToken(SourceLocation Loc, HighlightingKind Kind) { +const auto = AST.getSourceManager(); if (Loc.isMacroID()) { // Only intereseted in highlighting arguments in macros (DEF_X(arg)). if (!SM.isMacroArgExpansion(Loc)) @@ -279,7 +284,7 @@ private: if (!isInsideMainFile(Loc, SM)) return; -auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc); +auto R = getTokenRange(SM, AST.getASTContext().getLangOpts(), Loc); if (!R) { // R should always have a value, if it doesn't something is very wrong. elog("Tried to add semantic token with an invalid range"); @@ -466,6 +471,8 @@ llvm::StringRef toTextMateScope(Highligh return "entity.name.type.template.cpp"; case HighlightingKind::Primitive: return "storage.type.primitive.cpp"; + case HighlightingKind::Macro: +return "entity.name.function.preprocessor.cpp"; case HighlightingKind::NumKinds: llvm_unreachable("must not pass NumKinds to the function"); } Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=370482=370481=370482=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Fri Aug 30 08:47:27 2019 @@ -38,6 +38,7 @@ enum class HighlightingKind { Namespace, TemplateParameter, Primitive, + Macro, NumKinds, }; Modified: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/test/semantic-highlighting.test?rev=370482=370481=370482=diff == --- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test (original) +++ clang-tools-extra/trunk/clangd/test/semantic-highlighting.test Fri Aug 30 08:47:27 2019 @@ -45,6 +45,9 @@ # CHECK-NEXT: ], # CHECK-NEXT: [ # CHECK-NEXT:"storage.type.primitive.cpp" +# CHECK-NEXT: ], +# CHECK-NEXT: [ +# CHECK-NEXT:"entity.name.function.preprocessor.cpp" # CHECK-NEXT: ] # CHECK-NEXT:] # CHECK-NEXT: }, Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=370482=370481=370482=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Fri Aug 30 08:47:27 2019 @@ -47,7 +47,8 @@ std::vector getExpect {HighlightingKind::Method, "Method"},
[clang-tools-extra] r370473 - [clangd] Added highlighting for structured bindings.
Author: jvikstrom Date: Fri Aug 30 07:07:05 2019 New Revision: 370473 URL: http://llvm.org/viewvc/llvm-project?rev=370473=rev Log: [clangd] Added highlighting for structured bindings. Summary: Structured bindings are in a BindingDecl. The decl the declRefExpr points to are the BindingDecls. So this adds an additional if statement in the addToken function to highlight them. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66738 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=370473=370472=370473=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Fri Aug 30 07:07:05 2019 @@ -233,6 +233,10 @@ private: : HighlightingKind::Variable); return; } +if (isa(D)) { + addToken(Loc, HighlightingKind::Variable); + return; +} if (isa(D)) { addToken(Loc, HighlightingKind::Function); return; Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=370473=370472=370473=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Fri Aug 30 07:07:05 2019 @@ -436,6 +436,21 @@ TEST(SemanticHighlighting, GetsCorrectTo assert($Variable[[x]] != $Variable[[y]]); assert($Variable[[x]] != $Function[[f]]()); } +)cpp", +R"cpp( + struct $Class[[S]] { +$Primitive[[float]] $Field[[Value]]; +$Class[[S]] *$Field[[Next]]; + }; + $Class[[S]] $Variable[[Global]][2] = {$Class[[S]](), $Class[[S]]()}; + $Primitive[[void]] $Function[[f]]($Class[[S]] $Parameter[[P]]) { +$Primitive[[int]] $LocalVariable[[A]][2] = {1,2}; +auto [$Variable[[B1]], $Variable[[B2]]] = $LocalVariable[[A]]; +auto [$Variable[[G1]], $Variable[[G2]]] = $Variable[[Global]]; +$Class[[auto]] [$Variable[[P1]], $Variable[[P2]]] = $Parameter[[P]]; +// Highlights references to BindingDecls. +$Variable[[B1]]++; + } )cpp"}; for (const auto : TestCases) { checkHighlightings(TestCase); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r370452 - [clangd] Collecting main file macro expansion locations in ParsedAST.
Author: jvikstrom Date: Fri Aug 30 02:33:27 2019 New Revision: 370452 URL: http://llvm.org/viewvc/llvm-project?rev=370452=rev Log: [clangd] Collecting main file macro expansion locations in ParsedAST. Summary: TokenBuffer does not collect macro expansions inside macro arguments which is needed for semantic higlighting. Therefore collects macro expansions in the main file in a PPCallback when building the ParsedAST instead. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66928 Modified: clang-tools-extra/trunk/clangd/ClangdUnit.cpp clang-tools-extra/trunk/clangd/ClangdUnit.h clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp Modified: clang-tools-extra/trunk/clangd/ClangdUnit.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdUnit.cpp?rev=370452=370451=370452=diff == --- clang-tools-extra/trunk/clangd/ClangdUnit.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdUnit.cpp Fri Aug 30 02:33:27 2019 @@ -22,6 +22,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" #include "clang/Basic/LangOptions.h" +#include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/TokenKinds.h" #include "clang/Frontend/CompilerInstance.h" @@ -32,6 +33,7 @@ #include "clang/Index/IndexingAction.h" #include "clang/Lex/Lexer.h" #include "clang/Lex/MacroInfo.h" +#include "clang/Lex/PPCallbacks.h" #include "clang/Lex/Preprocessor.h" #include "clang/Lex/PreprocessorOptions.h" #include "clang/Sema/Sema.h" @@ -103,6 +105,28 @@ private: std::vector TopLevelDecls; }; +// CollectMainFileMacroExpansions and CollectMainFileMacros are two different +// classes as CollectMainFileMacroExpansions is only used when building the AST +// for the main file. CollectMainFileMacros is only used when building the +// preamble. +class CollectMainFileMacroExpansions : public PPCallbacks { + const SourceManager + std::vector + +public: + CollectMainFileMacroExpansions(const SourceManager , + std::vector ) + : SM(SM), MainFileMacroLocs(MainFileMacroLocs) {} + + virtual void MacroExpands(const Token , +const MacroDefinition , SourceRange Range, +const MacroArgs *Args) { +SourceLocation L = MacroNameTok.getLocation(); +if (!L.isMacroID() && isInsideMainFile(L, SM)) + MainFileMacroLocs.push_back(L); + } +}; + class CollectMainFileMacros : public PPCallbacks { public: explicit CollectMainFileMacros(const SourceManager , @@ -417,6 +441,11 @@ ParsedAST::build(std::unique_ptrgetPreprocessor().addPPCallbacks( collectIncludeStructureCallback(Clang->getSourceManager(), )); + // Collect the macro expansions in the main file. + std::vector MainFileMacroExpLocs; + Clang->getPreprocessor().addPPCallbacks( + std::make_unique( + Clang->getSourceManager(), MainFileMacroExpLocs)); // Copy over the includes from the preamble, then combine with the // non-preamble includes below. @@ -470,7 +499,8 @@ ParsedAST::build(std::unique_ptr ParsedAST::getLoc return LocalTopLevelDecls; } +llvm::ArrayRef ParsedAST::getMainFileExpansions() const { + return MainFileMacroExpLocs; +} + const std::vector ::getDiagnostics() const { return Diags; } std::size_t ParsedAST::getUsedBytes() const { @@ -565,11 +599,13 @@ ParsedAST::ParsedAST(std::shared_ptr Clang, std::unique_ptr Action, syntax::TokenBuffer Tokens, + std::vector MainFileMacroExpLocs, std::vector LocalTopLevelDecls, std::vector Diags, IncludeStructure Includes, CanonicalIncludes CanonIncludes) : Preamble(std::move(Preamble)), Clang(std::move(Clang)), Action(std::move(Action)), Tokens(std::move(Tokens)), + MainFileMacroExpLocs(std::move(MainFileMacroExpLocs)), Diags(std::move(Diags)), LocalTopLevelDecls(std::move(LocalTopLevelDecls)), Includes(std::move(Includes)), CanonIncludes(std::move(CanonIncludes)) { Modified: clang-tools-extra/trunk/clangd/ClangdUnit.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdUnit.h?rev=370452=370451=370452=diff == --- clang-tools-extra/trunk/clangd/ClangdUnit.h (original) +++ clang-tools-extra/trunk/clangd/ClangdUnit.h Fri Aug 30 02:33:27 2019 @@ -118,6 +118,9 @@ public: const IncludeStructure () const; const CanonicalIncludes () const; + /// The start locations of all macro expansions spelled inside the main file. + /// Does not include expansions from inside other macro expansions. + llvm::ArrayRef getMainFileExpansions() const; /// Tokens
[clang-tools-extra] r370305 - [clangd] Update themeRuleMatcher when color theme changes in vscode extension.
Author: jvikstrom Date: Thu Aug 29 01:40:17 2019 New Revision: 370305 URL: http://llvm.org/viewvc/llvm-project?rev=370305=rev Log: [clangd] Update themeRuleMatcher when color theme changes in vscode extension. Summary: Add event listener that listens to configuration changes and reloads the ThemeRuleMatcher when the theme changes. Right now it will not recolor the files, depends on the colorizer CL for that. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66406 Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts?rev=370305=370304=370305=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts (original) +++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts Thu Aug 29 01:40:17 2019 @@ -91,6 +91,13 @@ export class SemanticHighlightingFeature // highlighter being created. this.highlighter = new Highlighter(this.scopeLookupTable); this.subscriptions.push(vscode.Disposable.from(this.highlighter)); +// Adds a listener to reload the theme when it changes. +this.subscriptions.push( +vscode.workspace.onDidChangeConfiguration((conf) => { + if (!conf.affectsConfiguration('workbench.colorTheme')) +return; + this.loadCurrentTheme(); +})); this.loadCurrentTheme(); // Event handling for handling with TextDocuments/Editors lifetimes. this.subscriptions.push(vscode.window.onDidChangeVisibleTextEditors( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r370202 - [clangd] Cleans up the semantic highlighting resources if clangd stops.
Author: jvikstrom Date: Wed Aug 28 06:46:22 2019 New Revision: 370202 URL: http://llvm.org/viewvc/llvm-project?rev=370202=rev Log: [clangd] Cleans up the semantic highlighting resources if clangd stops. Summary: Disposes of the vscode listeners when clangd crashes and reuses the old highlighter when it restarts. The reason for reusing the highlighter is because this way the highlightings will not disappear as we won't have to dispose of them. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66743 Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts?rev=370202=370201=370202=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts (original) +++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts Wed Aug 28 06:46:22 2019 @@ -112,14 +112,6 @@ export function activate(context: vscode const semanticHighlightingFeature = new semanticHighlighting.SemanticHighlightingFeature(); clangdClient.registerFeature(semanticHighlightingFeature); - // The notification handler must be registered after the client is ready or - // the client will crash. - clangdClient.onReady().then( - () => clangdClient.onNotification( - semanticHighlighting.NotificationType, - semanticHighlightingFeature.handleNotification.bind( - semanticHighlightingFeature))); - console.log('Clang Language Server is now active!'); context.subscriptions.push(clangdClient.start()); context.subscriptions.push(vscode.commands.registerCommand( @@ -149,9 +141,14 @@ export function activate(context: vscode clangdClient.onNotification( 'textDocument/clangd.fileStatus', (fileStatus) => { status.onFileUpdated(fileStatus); }); + clangdClient.onNotification( + semanticHighlighting.NotificationType, + semanticHighlightingFeature.handleNotification.bind( + semanticHighlightingFeature)); } else if (newState == vscodelc.State.Stopped) { // Clear all cached statuses when clangd crashes. status.clear(); + semanticHighlightingFeature.dispose(); } }) // An empty place holder for the activate command, otherwise we'll get an Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts?rev=370202=370201=370202=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts (original) +++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts Wed Aug 28 06:46:22 2019 @@ -56,6 +56,8 @@ export class SemanticHighlightingFeature scopeLookupTable: string[][]; // The object that applies the highlightings clangd sends. highlighter: Highlighter; + // Any disposables that should be cleaned up when clangd crashes. + private subscriptions: vscode.Disposable[] = []; fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) { // Extend the ClientCapabilities type and add semantic highlighting // capability to the object. @@ -88,14 +90,15 @@ export class SemanticHighlightingFeature // otherwise it could try to update the themeRuleMatcher without the // highlighter being created. this.highlighter = new Highlighter(this.scopeLookupTable); +this.subscriptions.push(vscode.Disposable.from(this.highlighter)); this.loadCurrentTheme(); // Event handling for handling with TextDocuments/Editors lifetimes. -vscode.window.onDidChangeVisibleTextEditors( +this.subscriptions.push(vscode.window.onDidChangeVisibleTextEditors( (editors: vscode.TextEditor[]) => editors.forEach((e) => this.highlighter.applyHighlights( -e.document.uri.toString(; -vscode.workspace.onDidCloseTextDocument( -(doc) => this.highlighter.removeFileHighlightings(doc.uri.toString())); +e.document.uri.toString(); +this.subscriptions.push(vscode.workspace.onDidCloseTextDocument( +(doc) => this.highlighter.removeFileHighlightings(doc.uri.toString(; } handleNotification(params: SemanticHighlightingParams) { @@ -103,6 +106,11 @@ export class SemanticHighlightingFeature
[clang-tools-extra] r369911 - [clangd] Handling text editor/document lifetimes in vscode extension.
Author: jvikstrom Date: Mon Aug 26 06:56:45 2019 New Revision: 369911 URL: http://llvm.org/viewvc/llvm-project?rev=369911=rev Log: [clangd] Handling text editor/document lifetimes in vscode extension. Summary: Just reapplies highlightings for all files when visible text editors change. Could find the correct text editor that should be reapplied but going for a simple implementation instead. Removes the associated highlighting entry from the Colorizer when a text document is closed. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66735 Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts?rev=369911=369910=369911=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts (original) +++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts Mon Aug 26 06:56:45 2019 @@ -89,6 +89,13 @@ export class SemanticHighlightingFeature // highlighter being created. this.highlighter = new Highlighter(this.scopeLookupTable); this.loadCurrentTheme(); +// Event handling for handling with TextDocuments/Editors lifetimes. +vscode.window.onDidChangeVisibleTextEditors( +(editors: vscode.TextEditor[]) => +editors.forEach((e) => this.highlighter.applyHighlights( +e.document.uri.toString(; +vscode.workspace.onDidCloseTextDocument( +(doc) => this.highlighter.removeFileHighlightings(doc.uri.toString())); } handleNotification(params: SemanticHighlightingParams) { @@ -150,12 +157,8 @@ export class Highlighter { }; return vscode.window.createTextEditorDecorationType(options); }); -this.getVisibleTextEditorUris().forEach((fileUri) => { - // A TextEditor might not be a cpp file. So we must check we have - // highlightings for the file before applying them. - if (this.files.has(fileUri)) -this.applyHighlights(fileUri); -}) +this.getVisibleTextEditorUris().forEach((fileUri) => +this.applyHighlights(fileUri)); } // Adds incremental highlightings to the current highlightings for the file @@ -171,6 +174,13 @@ export class Highlighter { this.applyHighlights(fileUri); } + // Called when a text document is closed. Removes any highlighting entries for + // the text document that was closed. + public removeFileHighlightings(fileUri: string) { +// If there exists no entry the call to delete just returns false. +this.files.delete(fileUri); + } + // Gets the uris as strings for the currently visible text editors. protected getVisibleTextEditorUris(): string[] { return vscode.window.visibleTextEditors.map((e) => @@ -180,6 +190,11 @@ export class Highlighter { // Returns the ranges that should be used when decorating. Index i in the // range array has the decoration type at index i of this.decorationTypes. protected getDecorationRanges(fileUri: string): vscode.Range[][] { +if (!this.files.has(fileUri)) + // this.files should always have an entry for fileUri if we are here. But + // if there isn't one we don't want to crash the extension. This is also + // useful for tests. + return []; const lines: SemanticHighlightingLine[] = Array.from(this.files.get(fileUri).values()); const decorations: vscode.Range[][] = this.decorationTypes.map(() => []); @@ -194,7 +209,11 @@ export class Highlighter { } // Applies all the highlightings currently stored for a file with fileUri. - protected applyHighlights(fileUri: string) { + public applyHighlights(fileUri: string) { +if (!this.files.has(fileUri)) + // There are no highlightings for this file, must return early or will get + // out of bounds when applying the decorations below. + return; if (!this.decorationTypes.length) // Can't apply any decorations when there is no theme loaded. return; Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts?rev=369911=369910=369911=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts (original) +++
[clang-tools-extra] r369893 - [clangd] Added a colorizer to the vscode extension.
Author: jvikstrom Date: Mon Aug 26 04:36:11 2019 New Revision: 369893 URL: http://llvm.org/viewvc/llvm-project?rev=369893=rev Log: [clangd] Added a colorizer to the vscode extension. Summary: Adds the main colorizer component. It colorizes every time clangd sends a publishSemanticHighlighting notification. Every time it colorizes it does a full recolorization (removes all decorations from the editor and applies new ones). The reason for this is that all ranges for the same scope share a TextEditorDecorationType. This is due to TextEditorDecorationTypes being very expensive to create. The prototype used one DecorationType per range but that ran into very big performance problems (it took >100 ms to apply 600 lines of highlightings which froze the editor). This version does not share the problem of being extremly slow, but there is probably potential to optimize it even more. No document/texteditor lifecycle management code in this CL, that will come in the next one. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66219 Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts?rev=369893=369892=369893=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts (original) +++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/extension.ts Mon Aug 26 04:36:11 2019 @@ -1,5 +1,6 @@ import * as vscode from 'vscode'; import * as vscodelc from 'vscode-languageclient'; +import * as semanticHighlighting from './semantic-highlighting'; /** * Method to get workspace configuration option @@ -108,6 +109,17 @@ export function activate(context: vscode const clangdClient = new ClangdLanguageClient('Clang Language Server', serverOptions, clientOptions); + const semanticHighlightingFeature = + new semanticHighlighting.SemanticHighlightingFeature(); + clangdClient.registerFeature(semanticHighlightingFeature); + // The notification handler must be registered after the client is ready or + // the client will crash. + clangdClient.onReady().then( + () => clangdClient.onNotification( + semanticHighlighting.NotificationType, + semanticHighlightingFeature.handleNotification.bind( + semanticHighlightingFeature))); + console.log('Clang Language Server is now active!'); context.subscriptions.push(clangdClient.start()); context.subscriptions.push(vscode.commands.registerCommand( Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts?rev=369893=369892=369893=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts (original) +++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts Mon Aug 26 04:36:11 2019 @@ -34,6 +34,13 @@ interface SemanticHighlightingToken { // The TextMate scope index to the clangd scope lookup table. scopeIndex: number; } +// A line of decoded highlightings from the data clangd sent. +export interface SemanticHighlightingLine { + // The zero-based line position in the text document. + line: number; + // All SemanticHighlightingTokens on the line. + tokens: SemanticHighlightingToken[]; +} // Language server push notification providing the semantic highlighting // information for a text document. @@ -47,8 +54,8 @@ export class SemanticHighlightingFeature // The TextMate scope lookup table. A token with scope index i has the scopes // on index i in the lookup table. scopeLookupTable: string[][]; - // The rules for the current theme. - themeRuleMatcher: ThemeRuleMatcher; + // The object that applies the highlightings clangd sends. + highlighter: Highlighter; fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) { // Extend the ClientCapabilities type and add semantic highlighting // capability to the object. @@ -61,9 +68,10 @@ export class SemanticHighlightingFeature } async loadCurrentTheme() { -this.themeRuleMatcher = new ThemeRuleMatcher( +const themeRuleMatcher = new ThemeRuleMatcher( await loadTheme(vscode.workspace.getConfiguration('workbench') .get('colorTheme'))); +
r369385 - [Syntax] Added function to get macro expansion tokens to TokenBuffer.
Author: jvikstrom Date: Tue Aug 20 06:34:01 2019 New Revision: 369385 URL: http://llvm.org/viewvc/llvm-project?rev=369385=rev Log: [Syntax] Added function to get macro expansion tokens to TokenBuffer. Summary: Returns the first token in every mapping where the token is an identifier. This API is required to be able to highlight macro expansions in clangd. Reviewers: hokein, ilya-biryukov Subscribers: kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66470 Modified: cfe/trunk/include/clang/Tooling/Syntax/Tokens.h cfe/trunk/lib/Tooling/Syntax/Tokens.cpp cfe/trunk/unittests/Tooling/Syntax/TokensTest.cpp Modified: cfe/trunk/include/clang/Tooling/Syntax/Tokens.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Syntax/Tokens.h?rev=369385=369384=369385=diff == --- cfe/trunk/include/clang/Tooling/Syntax/Tokens.h (original) +++ cfe/trunk/include/clang/Tooling/Syntax/Tokens.h Tue Aug 20 06:34:01 2019 @@ -236,6 +236,16 @@ public: ///#pragma, etc. llvm::ArrayRef spelledTokens(FileID FID) const; + /// Get all tokens that expand a macro in \p FID. For the following input + /// #define FOO B + /// #define FOO2(X) int X + /// FOO2(XY) + /// int B; + /// FOO; + /// macroExpansions() returns {"FOO2", "FOO"} (from line 3 and 5 + /// respecitvely). + std::vector macroExpansions(FileID FID) const; + const SourceManager () const { return *SourceMgr; } std::string dumpForTests() const; Modified: cfe/trunk/lib/Tooling/Syntax/Tokens.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Syntax/Tokens.cpp?rev=369385=369384=369385=diff == --- cfe/trunk/lib/Tooling/Syntax/Tokens.cpp (original) +++ cfe/trunk/lib/Tooling/Syntax/Tokens.cpp Tue Aug 20 06:34:01 2019 @@ -232,6 +232,21 @@ TokenBuffer::expansionStartingAt(const s return E; } +std::vector +TokenBuffer::macroExpansions(FileID FID) const { + auto FileIt = Files.find(FID); + assert(FileIt != Files.end() && "file not tracked by token buffer"); + auto = FileIt->second; + std::vector Expansions; + auto = File.SpelledTokens; + for (auto Mapping : File.Mappings) { +const syntax::Token *Token = [Mapping.BeginSpelled]; +if (Token->kind() == tok::TokenKind::identifier) + Expansions.push_back(Token); + } + return Expansions; +} + std::vector syntax::tokenize(FileID FID, const SourceManager , const LangOptions ) { std::vector Tokens; Modified: cfe/trunk/unittests/Tooling/Syntax/TokensTest.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/Syntax/TokensTest.cpp?rev=369385=369384=369385=diff == --- cfe/trunk/unittests/Tooling/Syntax/TokensTest.cpp (original) +++ cfe/trunk/unittests/Tooling/Syntax/TokensTest.cpp Tue Aug 20 06:34:01 2019 @@ -755,4 +755,27 @@ TEST_F(TokenBufferTest, TokensToFileRang // We don't test assertion failures because death tests are slow. } +TEST_F(TokenBufferTest, macroExpansions) { + llvm::Annotations Code(R"cpp( +#define FOO B +#define FOO2 BA +#define CALL(X) int X +#define G CALL(FOO2) +int B; +$macro[[FOO]]; +$macro[[CALL]](A); +$macro[[G]]; + )cpp"); + recordTokens(Code.code()); + auto = *SourceMgr; + auto Expansions = Buffer.macroExpansions(SM.getMainFileID()); + std::vector ExpectedMacroRanges; + for (auto Range : Code.ranges("macro")) +ExpectedMacroRanges.push_back( +FileRange(SM.getMainFileID(), Range.Begin, Range.End)); + std::vector ActualMacroRanges; + for (auto Expansion : Expansions) +ActualMacroRanges.push_back(Expansion->range(SM)); + EXPECT_EQ(ExpectedMacroRanges, ActualMacroRanges); +} } // namespace ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r369275 - [clangd] Added highlighting for tokens that are macro arguments.
Author: jvikstrom Date: Mon Aug 19 09:27:49 2019 New Revision: 369275 URL: http://llvm.org/viewvc/llvm-project?rev=369275=rev Log: [clangd] Added highlighting for tokens that are macro arguments. Summary: Adds semantic highlighting for tokens that are a macro argument. Example: ``` D_V(SomeVar); ``` The "SomeVar" inside the macro is highlighted as a variable now. Tokens that are in a macro body expansion are ignored in this patch for three reasons. * The spelling loc is inside the macro "definition" meaning it would highlight inside the macro definition (could probably easily be fixed by using getExpansionLoc instead of getSpellingLoc?) * If wanting to highlight the macro definition this could create duplicate tokens. And if the tokens are of different types there would be conflicts (tokens in the same range but with different types). Say a macro defines some name and both a variable declaration and a function use this, there would be two tokens in the macro definition but one with Kind "Variable" and the other with Kind "Function". * Thirdly, macro body expansions could come from a file that is not the main file (easily fixed, just check that the Loc is in the main file and not even a problem if we wanted to highlight the actual macro "invocation") Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64741 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=369275=369274=369275=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Mon Aug 19 09:27:49 2019 @@ -39,7 +39,27 @@ public: llvm::sort(Tokens); auto Last = std::unique(Tokens.begin(), Tokens.end()); Tokens.erase(Last, Tokens.end()); -return Tokens; +// Macros can give tokens that have the same source range but conflicting +// kinds. In this case all tokens sharing this source range should be +// removed. +std::vector NonConflicting; +NonConflicting.reserve(Tokens.size()); +for (ArrayRef TokRef = Tokens; !TokRef.empty();) { + ArrayRef Conflicting = + TokRef.take_while([&](const HighlightingToken ) { +// TokRef is guaranteed at least one element here because otherwise +// this predicate would never fire. +return T.R == TokRef.front().R; + }); + // If there is exactly one token with this range it's non conflicting and + // should be in the highlightings. + if (Conflicting.size() == 1) +NonConflicting.push_back(TokRef.front()); + // TokRef[Conflicting.size()] is the next token with a different range (or + // the end of the Tokens). + TokRef = TokRef.drop_front(Conflicting.size()); +} +return NonConflicting; } bool VisitNamespaceAliasDecl(NamespaceAliasDecl *NAD) { @@ -236,13 +256,18 @@ private: } void addToken(SourceLocation Loc, HighlightingKind Kind) { -if (Loc.isMacroID()) - // FIXME: skip tokens inside macros for now. - return; +if(Loc.isMacroID()) { + // Only intereseted in highlighting arguments in macros (DEF_X(arg)). + if (!SM.isMacroArgExpansion(Loc)) +return; + Loc = SM.getSpellingLoc(Loc); +} // Non top level decls that are included from a header are not filtered by // topLevelDecls. (example: method declarations being included from another // file for a class from another file) +// There are also cases with macros where the spelling loc will not be in the +// main file and the highlighting would be incorrect. if (!isInsideMainFile(Loc, SM)) return; Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=369275=369274=369275=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Mon Aug 19 09:27:49 2019 @@ -16,10 +16,6 @@ namespace clang { namespace clangd { - void PrintTo(const HighlightingToken , ::std::ostream *OS) { - *OS << "(" << T.R.start.line << ", " << T.R.start.character << ") -> (" << T.R.end.line << ", " << T.R.end.character << "): " << (int)T.Kind; -} - namespace { std::vector @@ -380,6 +376,56 @@ TEST(SemanticHighlighting, GetsCorrectTo $Class[[G]]<$Class[[F]],
[clang-tools-extra] r369238 - [clangd] Added special HighlightingKind for function parameters.
Author: jvikstrom Date: Mon Aug 19 00:51:39 2019 New Revision: 369238 URL: http://llvm.org/viewvc/llvm-project?rev=369238=rev Log: [clangd] Added special HighlightingKind for function parameters. Summary: This means that function parameters are no longer highlighted as variable.other.cpp but instead as variable.parameter.cpp which is the more "correct" TextMate scope for them. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66335 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/SemanticHighlighting.h clang-tools-extra/trunk/clangd/test/semantic-highlighting.test clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=369238=369237=369238=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Mon Aug 19 00:51:39 2019 @@ -11,6 +11,7 @@ #include "Protocol.h" #include "SourceCode.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/RecursiveASTVisitor.h" #include @@ -200,6 +201,10 @@ private: addToken(Loc, HighlightingKind::EnumConstant); return; } +if (isa(D)) { + addToken(Loc, HighlightingKind::Parameter); + return; +} if (isa(D)) { addToken(Loc, HighlightingKind::Variable); return; @@ -406,6 +411,8 @@ llvm::StringRef toTextMateScope(Highligh return "entity.name.function.method.cpp"; case HighlightingKind::Variable: return "variable.other.cpp"; + case HighlightingKind::Parameter: +return "variable.parameter.cpp"; case HighlightingKind::Field: return "variable.other.field.cpp"; case HighlightingKind::Class: Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=369238=369237=369238=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Mon Aug 19 00:51:39 2019 @@ -25,6 +25,7 @@ namespace clangd { enum class HighlightingKind { Variable = 0, + Parameter, Function, Method, Field, Modified: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/test/semantic-highlighting.test?rev=369238=369237=369238=diff == --- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test (original) +++ clang-tools-extra/trunk/clangd/test/semantic-highlighting.test Mon Aug 19 00:51:39 2019 @@ -8,6 +8,9 @@ # CHECK-NEXT:"variable.other.cpp" # CHECK-NEXT: ], # CHECK-NEXT: [ +# CHECK-NEXT:"variable.parameter.cpp" +# CHECK-NEXT: ], +# CHECK-NEXT: [ # CHECK-NEXT:"entity.name.function.cpp" # CHECK-NEXT: ], # CHECK-NEXT: [ @@ -43,7 +46,7 @@ # CHECK-NEXT:"lines": [ # CHECK-NEXT: { # CHECK-NEXT:"line": 0, -# CHECK-NEXT:"tokens": "AAADAAkEAAEAAA==" +# CHECK-NEXT:"tokens": "AAADAAoEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT:], # CHECK-NEXT:"textDocument": { @@ -58,11 +61,11 @@ # CHECK-NEXT:"lines": [ # CHECK-NEXT: { # CHECK-NEXT:"line": 0, -# CHECK-NEXT:"tokens": "AAADAAkEAAEAAA==" +# CHECK-NEXT:"tokens": "AAADAAoEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT: { # CHECK-NEXT:"line": 1, -# CHECK-NEXT:"tokens": "AAADAAkEAAEAAA==" +# CHECK-NEXT:"tokens": "AAADAAoEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT:], # CHECK-NEXT:"textDocument": { @@ -77,7 +80,7 @@ # CHECK-NEXT:"lines": [ # CHECK-NEXT: { # CHECK-NEXT:"line": 1, -# CHECK-NEXT:"tokens": "AAADAAkEAAEAAA==" +# CHECK-NEXT:"tokens": "AAADAAoEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT: ], # CHECK-NEXT:"textDocument": { Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=369238=369237=369238=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++
[clang-tools-extra] r369090 - [clangd] Added highlighting for non type templates.
Author: jvikstrom Date: Fri Aug 16 02:30:21 2019 New Revision: 369090 URL: http://llvm.org/viewvc/llvm-project?rev=369090=rev Log: [clangd] Added highlighting for non type templates. Summary: Non type templates were not being highlighted. This highlights them as TemplateParameters. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66221 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=369090=369089=369090=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Fri Aug 16 02:30:21 2019 @@ -224,6 +224,10 @@ private: addToken(Loc, HighlightingKind::TemplateParameter); return; } +if (isa(D)) { + addToken(Loc, HighlightingKind::TemplateParameter); + return; +} } void addToken(SourceLocation Loc, HighlightingKind Kind) { Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=369090=369089=369090=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Fri Aug 16 02:30:21 2019 @@ -323,6 +323,59 @@ TEST(SemanticHighlighting, GetsCorrectTo $Primitive[[auto]] $Variable[[Form]] = 10.2 + 2 * 4; $Primitive[[decltype]]($Variable[[Form]]) $Variable[[F]] = 10; auto $Variable[[Fun]] = []()->$Primitive[[void]]{}; +)cpp", +R"cpp( + class $Class[[G]] {}; + template<$Class[[G]] *$TemplateParameter[[U]]> + class $Class[[GP]] {}; + template<$Class[[G]] &$TemplateParameter[[U]]> + class $Class[[GR]] {}; + template<$Primitive[[int]] *$TemplateParameter[[U]]> + class $Class[[IP]] { +$Primitive[[void]] $Method[[f]]() { + *$TemplateParameter[[U]] += 5; +} + }; + template<$Primitive[[unsigned]] $TemplateParameter[[U]] = 2> + class $Class[[Foo]] { +$Primitive[[void]] $Method[[f]]() { + for($Primitive[[int]] $Variable[[I]] = 0; +$Variable[[I]] < $TemplateParameter[[U]];) {} +} + }; + + $Class[[G]] $Variable[[L]]; + $Primitive[[void]] $Function[[f]]() { +$Class[[Foo]]<123> $Variable[[F]]; +$Class[[GP]]<&$Variable[[L]]> $Variable[[LL]]; +$Class[[GR]]<$Variable[[L]]> $Variable[[LLL]]; + } +)cpp", +R"cpp( + template + struct $Class[[G]] { +$Primitive[[void]] $Method[[foo]]( +$TemplateParameter[[T]] *$Variable[[O]]) { + ($Variable[[O]]->*$TemplateParameter[[method]])(10); +} + }; + struct $Class[[F]] { +$Primitive[[void]] $Method[[f]]($Primitive[[int]]); + }; + template<$Primitive[[void]] (*$TemplateParameter[[Func]])()> + struct $Class[[A]] { +$Primitive[[void]] $Method[[f]]() { + (*$TemplateParameter[[Func]])(); +} + }; + + $Primitive[[void]] $Function[[foo]]() { +$Class[[F]] $Variable[[FF]]; +$Class[[G]]<$Class[[F]], &$Class[[F]]::$Method[[f]]> $Variable[[GG]]; +$Variable[[GG]].$Method[[foo]](&$Variable[[FF]]); +$Class[[A]]<$Function[[foo]]> $Variable[[AA]]; + } )cpp"}; for (const auto : TestCases) { checkHighlightings(TestCase); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r368834 - [clangd] Loading TokenColorRules as a class mapping the rules to their associated clangd TextMate scope index.
Author: jvikstrom Date: Wed Aug 14 05:11:58 2019 New Revision: 368834 URL: http://llvm.org/viewvc/llvm-project?rev=368834=rev Log: [clangd] Loading TokenColorRules as a class mapping the rules to their associated clangd TextMate scope index. Summary: Loads a mapping of the clangd scope lookup table scopes to the most specific rule with the highest "precedence" on initialize. Preprocesses into a class so it's simple/fast to access when doing the actual coloring later. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65856 Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts?rev=368834=368833=368834=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts (original) +++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts Wed Aug 14 05:11:58 2019 @@ -47,6 +47,8 @@ export class SemanticHighlightingFeature // The TextMate scope lookup table. A token with scope index i has the scopes // on index i in the lookup table. scopeLookupTable: string[][]; + // The rules for the current theme. + themeRuleMatcher: ThemeRuleMatcher; fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) { // Extend the ClientCapabilities type and add semantic highlighting // capability to the object. @@ -58,6 +60,12 @@ export class SemanticHighlightingFeature }; } + async loadCurrentTheme() { +this.themeRuleMatcher = new ThemeRuleMatcher( +await loadTheme(vscode.workspace.getConfiguration('workbench') +.get('colorTheme'))); + } + initialize(capabilities: vscodelc.ServerCapabilities, documentSelector: vscodelc.DocumentSelector|undefined) { // The semantic highlighting capability information is in the capabilities @@ -68,6 +76,7 @@ export class SemanticHighlightingFeature if (!serverCapabilities.semanticHighlighting) return; this.scopeLookupTable = serverCapabilities.semanticHighlighting.scopes; +this.loadCurrentTheme(); } handleNotification(params: SemanticHighlightingParams) {} @@ -101,6 +110,39 @@ interface TokenColorRule { foreground: string; } +export class ThemeRuleMatcher { + // The rules for the theme. + private themeRules: TokenColorRule[]; + // A cache for the getBestThemeRule function. + private bestRuleCache: Map = new Map(); + constructor(rules: TokenColorRule[]) { this.themeRules = rules; } + // Returns the best rule for a scope. + getBestThemeRule(scope: string): TokenColorRule { +if (this.bestRuleCache.has(scope)) + return this.bestRuleCache.get(scope); +let bestRule: TokenColorRule = {scope : '', foreground : ''}; +this.themeRules.forEach((rule) => { + // The best rule for a scope is the rule that is the longest prefix of the + // scope (unless a perfect match exists in which case the perfect match is + // the best). If a rule is not a prefix and we tried to match with longest + // common prefix instead variables would be highlighted as `less` + // variables when using Light+ (as variable.other would be matched against + // variable.other.less in this case). Doing common prefix matching also + // means we could match variable.cpp to variable.css if variable.css + // occurs before variable in themeRules. + // FIXME: This is not defined in the TextMate standard (it is explicitly + // undefined, https://macromates.com/manual/en/scope_selectors). Might + // want to rank some other way. + if (scope.startsWith(rule.scope) && + rule.scope.length > bestRule.scope.length) +// This rule matches and is more specific than the old rule. +bestRule = rule; +}); +this.bestRuleCache.set(scope, bestRule); +return bestRule; + } +} + // Get all token color rules provided by the theme. function loadTheme(themeName: string): Promise { const extension = Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts?rev=368834=368833=368834=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts (original) +++
[clang-tools-extra] r368568 - [clangd] Added the vscode SemanticHighlighting feature code but did not enable it in the client.
Author: jvikstrom Date: Mon Aug 12 06:33:43 2019 New Revision: 368568 URL: http://llvm.org/viewvc/llvm-project?rev=368568=rev Log: [clangd] Added the vscode SemanticHighlighting feature code but did not enable it in the client. Summary: Added the code for the StaticFeature that must be registered to the client. Also decoding the notification data into objects. Did not register it to the client yet. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65998 Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json?rev=368568=368567=368568=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json (original) +++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json Mon Aug 12 06:33:43 2019 @@ -38,7 +38,8 @@ "dependencies": { "jsonc-parser": "^2.1.0", "vscode-languageclient": "^5.3.0-next.6", -"vscode-languageserver": "^5.3.0-next.6" +"vscode-languageserver": "^5.3.0-next.6", +"vscode-languageserver-types": "^3.14.0" }, "devDependencies": { "@types/mocha": "^2.2.32", Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts?rev=368568=368567=368568=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts (original) +++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts Mon Aug 12 06:33:43 2019 @@ -2,6 +2,95 @@ import * as fs from 'fs'; import * as jsonc from "jsonc-parser"; import * as path from 'path'; import * as vscode from 'vscode'; +import * as vscodelc from 'vscode-languageclient'; +import * as vscodelct from 'vscode-languageserver-types'; + +// Parameters for the semantic highlighting (server-side) push notification. +// Mirrors the structure in the semantic highlighting proposal for LSP. +interface SemanticHighlightingParams { + // The text document that has to be decorated with the semantic highlighting + // information. + textDocument: vscodelct.VersionedTextDocumentIdentifier; + // An array of semantic highlighting information. + lines: SemanticHighlightingInformation[]; +} +// Contains the highlighting information for a specified line. Mirrors the +// structure in the semantic highlighting proposal for LSP. +interface SemanticHighlightingInformation { + // The zero-based line position in the text document. + line: number; + // A base64 encoded string representing every single highlighted characters + // with its start position, length and the "lookup table" index of of the + // semantic highlighting Text Mate scopes. + tokens?: string; +} + +// A SemanticHighlightingToken decoded from the base64 data sent by clangd. +interface SemanticHighlightingToken { + // Start column for this token. + character: number; + // Length of the token. + length: number; + // The TextMate scope index to the clangd scope lookup table. + scopeIndex: number; +} + +// Language server push notification providing the semantic highlighting +// information for a text document. +export const NotificationType = +new vscodelc.NotificationType( +'textDocument/semanticHighlighting'); + +// The feature that should be registered in the vscode lsp for enabling +// experimental semantic highlighting. +export class SemanticHighlightingFeature implements vscodelc.StaticFeature { + // The TextMate scope lookup table. A token with scope index i has the scopes + // on index i in the lookup table. + scopeLookupTable: string[][]; + fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) { +// Extend the ClientCapabilities type and add semantic highlighting +// capability to the object. +const textDocumentCapabilities: vscodelc.TextDocumentClientCapabilities& +{semanticHighlightingCapabilities?: {semanticHighlighting : boolean}} = +capabilities.textDocument; +textDocumentCapabilities.semanticHighlightingCapabilities = { + semanticHighlighting : true, +}; + } + + initialize(capabilities: vscodelc.ServerCapabilities, + documentSelector: vscodelc.DocumentSelector|undefined) { +// The semantic highlighting capability information is in the capabilities +// object but to
[clang-tools-extra] r368563 - [clangd] Remove highlightings coming from non topLevelDecls from included files.
Author: jvikstrom Date: Mon Aug 12 06:01:11 2019 New Revision: 368563 URL: http://llvm.org/viewvc/llvm-project?rev=368563=rev Log: [clangd] Remove highlightings coming from non topLevelDecls from included files. Summary: It is possible to write include code from other files so that the decls from there do not become topLevelDecls (For example by including methods for a class). These Decls are not filtered by topLevelDecls and therefore SemanticHighlighting must manually check that every SLoc belongs in the main file. Otherwise there can be highlightings appearing in places where they should not. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66083 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=368563=368562=368563=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Mon Aug 12 06:01:11 2019 @@ -231,6 +231,12 @@ private: // FIXME: skip tokens inside macros for now. return; +// Non top level decls that are included from a header are not filtered by +// topLevelDecls. (example: method declarations being included from another +// file for a class from another file) +if (!isInsideMainFile(Loc, SM)) + return; + auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc); if (!R) { // R should always have a value, if it doesn't something is very wrong. Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=368563=368562=368563=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Mon Aug 12 06:01:11 2019 @@ -51,9 +51,15 @@ std::vector getExpect return ExpectedTokens; } -void checkHighlightings(llvm::StringRef Code) { +void checkHighlightings(llvm::StringRef Code, +std::vector> +AdditionalFiles = {}) { Annotations Test(Code); - auto AST = TestTU::withCode(Test.code()).build(); + auto TU = TestTU::withCode(Test.code()); + for (auto File : AdditionalFiles) +TU.AdditionalFiles.insert({File.first, File.second}); + auto AST = TU.build(); std::vector ActualTokens = getSemanticHighlightings(AST); EXPECT_THAT(ActualTokens, getExpectedTokens(Test)) << Code; } @@ -321,6 +327,17 @@ TEST(SemanticHighlighting, GetsCorrectTo for (const auto : TestCases) { checkHighlightings(TestCase); } + + checkHighlightings(R"cpp( +class $Class[[A]] { + #include "imp.h" +}; +#endif + )cpp", + {{"imp.h", R"cpp( +int someMethod(); +void otherMethod(); + )cpp"}}); } TEST(SemanticHighlighting, GeneratesHighlightsWhenFileChange) { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r368546 - [clangd] Highlighting auto variables as the deduced type.
Author: jvikstrom Date: Mon Aug 12 00:45:12 2019 New Revision: 368546 URL: http://llvm.org/viewvc/llvm-project?rev=368546=rev Log: [clangd] Highlighting auto variables as the deduced type. Summary: Done in VisitDeclaratorDecl as the AutoTypeLoc is not deduced. Scoped to only work for variables. auto function return values need to be handled in a special way (separate patch for that). auto's that are in lambdas ar enot highlighted as we don't highlight their underlying type (it's a RecordDecl, but the name is not an identifier so it returns in addToken). Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65996 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=368546=368545=368546=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Mon Aug 12 00:45:12 2019 @@ -97,8 +97,8 @@ public: } bool VisitTypedefNameDecl(TypedefNameDecl *TD) { -if(const auto *TSI = TD->getTypeSourceInfo()) - addTypeLoc(TD->getLocation(), TSI->getTypeLoc()); +if (const auto *TSI = TD->getTypeSourceInfo()) + addType(TD->getLocation(), TSI->getTypeLoc().getTypePtr()); return true; } @@ -120,10 +120,8 @@ public: // structs. It also makes us not highlight certain namespace qualifiers // twice. For elaborated types the actual type is highlighted as an inner // TypeLoc. -if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated) - return true; - -addTypeLoc(TL.getBeginLoc(), TL); +if (TL.getTypeLocClass() != TypeLoc::TypeLocClass::Elaborated) + addType(TL.getBeginLoc(), TL.getTypePtr()); return true; } @@ -144,15 +142,27 @@ public: HighlightingTokenCollector>::TraverseConstructorInitializer(CI); } -private: - void addTypeLoc(SourceLocation Loc, const TypeLoc ) { -if (const Type *TP = TL.getTypePtr()) { - if (const TagDecl *TD = TP->getAsTagDecl()) -addToken(Loc, TD); - if (TP->isBuiltinType()) -// Builtins must be special cased as they do not have a TagDecl. -addToken(Loc, HighlightingKind::Primitive); + bool VisitDeclaratorDecl(DeclaratorDecl *D) { +if ((!D->getTypeSourceInfo())) + return true; + +if (auto *AT = D->getType()->getContainedAutoType()) { + const auto Deduced = AT->getDeducedType(); + if (!Deduced.isNull()) +addType(D->getTypeSpecStartLoc(), Deduced.getTypePtr()); } +return true; + } + +private: + void addType(SourceLocation Loc, const Type *TP) { +if (!TP) + return; +if (TP->isBuiltinType()) + // Builtins must be special cased as they do not have a TagDecl. + addToken(Loc, HighlightingKind::Primitive); +if (const TagDecl *TD = TP->getAsTagDecl()) + addToken(Loc, TD); } void addToken(SourceLocation Loc, const NamedDecl *D) { Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=368546=368545=368546=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Mon Aug 12 00:45:12 2019 @@ -99,9 +99,9 @@ TEST(SemanticHighlighting, GetsCorrectTo struct { } $Variable[[S]]; $Primitive[[void]] $Function[[foo]]($Primitive[[int]] $Variable[[A]], $Class[[AS]] $Variable[[As]]) { -auto $Variable[[VeryLongVariableName]] = 12312; +$Primitive[[auto]] $Variable[[VeryLongVariableName]] = 12312; $Class[[AS]] $Variable[[AA]]; -auto $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Variable[[A]]; +$Primitive[[auto]] $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Variable[[A]]; auto $Variable[[FN]] = [ $Variable[[AA]]]($Primitive[[int]] $Variable[[A]]) -> $Primitive[[void]] {}; $Variable[[FN]](12312); } @@ -303,6 +303,20 @@ TEST(SemanticHighlighting, GetsCorrectTo class $Class[[Bar2]] : public $Class[[Bar]] { $Class[[Bar2]]() : $Class[[Bar]]($Class[[Foo]](), $EnumConstant[[EC]]) {} }; +)cpp", +R"cpp( + enum $Enum[[E]] { +$EnumConstant[[E]], + }; + class $Class[[Foo]] {}; + $Enum[[auto]] $Variable[[AE]] = $Enum[[E]]::$EnumConstant[[E]]; + $Class[[auto]] $Variable[[AF]] = $Class[[Foo]](); +
[clang-tools-extra] r368434 - [clangd] Added highlighting for constructor initializers.
Author: jvikstrom Date: Fri Aug 9 05:19:10 2019 New Revision: 368434 URL: http://llvm.org/viewvc/llvm-project?rev=368434=rev Log: [clangd] Added highlighting for constructor initializers. Summary: Constructor initializers were not being highlighted. This adds highlighting for them by using TraverseConstructorInitializer. Uses the Traverse* because there is no visit for CXXCtorInitializer. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66001 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=368434=368433=368434=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Fri Aug 9 05:19:10 2019 @@ -11,6 +11,7 @@ #include "Protocol.h" #include "SourceCode.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/DeclCXX.h" #include "clang/AST/RecursiveASTVisitor.h" #include @@ -136,6 +137,13 @@ public: HighlightingTokenCollector>::TraverseNestedNameSpecifierLoc(NNSLoc); } + bool TraverseConstructorInitializer(CXXCtorInitializer *CI) { +if (const FieldDecl *FD = CI->getMember()) + addToken(CI->getSourceLocation(), FD); +return RecursiveASTVisitor< +HighlightingTokenCollector>::TraverseConstructorInitializer(CI); + } + private: void addTypeLoc(SourceLocation Loc, const TypeLoc ) { if (const Type *TP = TL.getTypePtr()) { Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=368434=368433=368434=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Fri Aug 9 05:19:10 2019 @@ -285,6 +285,24 @@ TEST(SemanticHighlighting, GetsCorrectTo }; $Class[[A]] &$Class[[A]]::operator=($Class[[A]] &&$Variable[[O]]) = default; +)cpp", +R"cpp( + enum $Enum[[En]] { +$EnumConstant[[EC]], + }; + class $Class[[Foo]] {}; + class $Class[[Bar]] { +$Class[[Foo]] $Field[[Fo]]; +$Enum[[En]] $Field[[E]]; +$Primitive[[int]] $Field[[I]]; +$Class[[Bar]] ($Class[[Foo]] $Variable[[F]], +$Enum[[En]] $Variable[[E]]) +: $Field[[Fo]] ($Variable[[F]]), $Field[[E]] ($Variable[[E]]), + $Field[[I]] (123) {} + }; + class $Class[[Bar2]] : public $Class[[Bar]] { +$Class[[Bar2]]() : $Class[[Bar]]($Class[[Foo]](), $EnumConstant[[EC]]) {} + }; )cpp"}; for (const auto : TestCases) { checkHighlightings(TestCase); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r368403 - [clangd] Fixed printTemplateSpecializationArgs not printing partial variable specialization arguments.
Author: jvikstrom Date: Fri Aug 9 00:35:16 2019 New Revision: 368403 URL: http://llvm.org/viewvc/llvm-project?rev=368403=rev Log: [clangd] Fixed printTemplateSpecializationArgs not printing partial variable specialization arguments. Summary: printTemplateSpecializationArgs was not printing partial variable specialization args. This adds an additional If clause where we check if it's a VariableTemplatePartialSpecializationDecl and returns the ArgumentLocs if that's the case. Also adds tests for printTemplateSpecializationArgs in ASTTests.cpp. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65926 Modified: clang-tools-extra/trunk/clangd/AST.cpp clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp clang-tools-extra/trunk/clangd/unittests/PrintASTTests.cpp Modified: clang-tools-extra/trunk/clangd/AST.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/AST.cpp?rev=368403=368402=368403=diff == --- clang-tools-extra/trunk/clangd/AST.cpp (original) +++ clang-tools-extra/trunk/clangd/AST.cpp Fri Aug 9 00:35:16 2019 @@ -36,6 +36,10 @@ getTemplateSpecializationArgLocs(const N llvm::dyn_cast()) { if (auto *Args = Cls->getTemplateArgsAsWritten()) return Args->arguments(); + } else if (auto *Var = + llvm::dyn_cast()) { +if (auto *Args = Var->getTemplateArgsAsWritten()) + return Args->arguments(); } else if (auto *Var = llvm::dyn_cast()) return Var->getTemplateArgsInfo().arguments(); // We return None for ClassTemplateSpecializationDecls because it does not Modified: clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp?rev=368403=368402=368403=diff == --- clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp Fri Aug 9 00:35:16 2019 @@ -188,7 +188,7 @@ TEST(ClangdUnitTest, AllOf(DeclNamed("foo"), WithTemplateArgs("")), AllOf(DeclNamed("i"), WithTemplateArgs("")), AllOf(DeclNamed("d"), WithTemplateArgs("")), -AllOf(DeclNamed("foo"), WithTemplateArgs("<>")), +AllOf(DeclNamed("foo"), WithTemplateArgs("")), AllOf(DeclNamed("foo"), WithTemplateArgs(""))})); } Modified: clang-tools-extra/trunk/clangd/unittests/PrintASTTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/PrintASTTests.cpp?rev=368403=368402=368403=diff == --- clang-tools-extra/trunk/clangd/unittests/PrintASTTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/PrintASTTests.cpp Fri Aug 9 00:35:16 2019 @@ -95,6 +95,15 @@ INSTANTIATE_TEST_CASE_P(ASTUtilsTests, A struct Bar { friend class Foo; }; template <> struct ^Foo {};)cpp", {""}}, +{ +R"cpp( + template + T S = T(10); + template + int ^S = 0; + template <> + int ^S = 0;)cpp", +{"", ""}}, })),); } // namespace } // namespace clangd ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r368402 - [AST] No longer visiting CXXMethodDecl bodies created by compiler when method was default created.
Author: jvikstrom Date: Fri Aug 9 00:30:28 2019 New Revision: 368402 URL: http://llvm.org/viewvc/llvm-project?rev=368402=rev Log: [AST] No longer visiting CXXMethodDecl bodies created by compiler when method was default created. Summary: Clang generates function bodies and puts them in the AST for default methods if it is defaulted outside the class definition. ` struct A { A =(A &); }; A ::operator=(A &) = default; ` This will generate a function body for the `A ::operator=(A &)` and put it in the AST. This body should not be visited if implicit code is not visited as it is implicit. This was causing SemanticHighlighting in clangd to generate duplicate tokens and putting them in weird places. Reviewers: hokein, ilya-biryukov, gribozavr Subscribers: mgorny, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65938 Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=368402=368401=368402=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Fri Aug 9 00:30:28 2019 @@ -55,7 +55,7 @@ void checkHighlightings(llvm::StringRef Annotations Test(Code); auto AST = TestTU::withCode(Test.code()).build(); std::vector ActualTokens = getSemanticHighlightings(AST); - EXPECT_THAT(ActualTokens, getExpectedTokens(Test)); + EXPECT_THAT(ActualTokens, getExpectedTokens(Test)) << Code; } // Any annotations in OldCode and NewCode are converted into their corresponding @@ -276,6 +276,15 @@ TEST(SemanticHighlighting, GetsCorrectTo $Class[[Foo]] *$Variable[[FP]] = ($Class[[Foo]]*)$Variable[[B]]; $Primitive[[int]] $Variable[[I]] = ($Primitive[[int]])$Variable[[B]]; } +)cpp" +R"cpp( + struct $Class[[B]] {}; + struct $Class[[A]] { +$Class[[B]] $Field[[BB]]; +$Class[[A]] =($Class[[A]] &&$Variable[[O]]); + }; + + $Class[[A]] &$Class[[A]]::operator=($Class[[A]] &&$Variable[[O]]) = default; )cpp"}; for (const auto : TestCases) { checkHighlightings(TestCase); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
r368402 - [AST] No longer visiting CXXMethodDecl bodies created by compiler when method was default created.
Author: jvikstrom Date: Fri Aug 9 00:30:28 2019 New Revision: 368402 URL: http://llvm.org/viewvc/llvm-project?rev=368402=rev Log: [AST] No longer visiting CXXMethodDecl bodies created by compiler when method was default created. Summary: Clang generates function bodies and puts them in the AST for default methods if it is defaulted outside the class definition. ` struct A { A =(A &); }; A ::operator=(A &) = default; ` This will generate a function body for the `A ::operator=(A &)` and put it in the AST. This body should not be visited if implicit code is not visited as it is implicit. This was causing SemanticHighlighting in clangd to generate duplicate tokens and putting them in weird places. Reviewers: hokein, ilya-biryukov, gribozavr Subscribers: mgorny, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65938 Added: cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/CXXMethodDecl.cpp Modified: cfe/trunk/include/clang/AST/RecursiveASTVisitor.h cfe/trunk/unittests/Tooling/CMakeLists.txt Modified: cfe/trunk/include/clang/AST/RecursiveASTVisitor.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/RecursiveASTVisitor.h?rev=368402=368401=368402=diff == --- cfe/trunk/include/clang/AST/RecursiveASTVisitor.h (original) +++ cfe/trunk/include/clang/AST/RecursiveASTVisitor.h Fri Aug 9 00:30:28 2019 @@ -2027,7 +2027,13 @@ bool RecursiveASTVisitor::Trave } } - if (D->isThisDeclarationADefinition()) { + bool VisitBody = D->isThisDeclarationADefinition(); + // If a method is set to default outside the class definition the compiler + // generates the method body and adds it to the AST. + if (const auto *MD = dyn_cast(D)) +VisitBody &= !MD->isDefaulted() || getDerived().shouldVisitImplicitCode(); + + if (VisitBody) { TRY_TO(TraverseStmt(D->getBody())); // Function body. } return true; Modified: cfe/trunk/unittests/Tooling/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/CMakeLists.txt?rev=368402=368401=368402=diff == --- cfe/trunk/unittests/Tooling/CMakeLists.txt (original) +++ cfe/trunk/unittests/Tooling/CMakeLists.txt Fri Aug 9 00:30:28 2019 @@ -28,6 +28,7 @@ add_clang_unittest(ToolingTests RecursiveASTVisitorTests/ConstructExpr.cpp RecursiveASTVisitorTests/CXXBoolLiteralExpr.cpp RecursiveASTVisitorTests/CXXMemberCall.cpp + RecursiveASTVisitorTests/CXXMethodDecl.cpp RecursiveASTVisitorTests/CXXOperatorCallExprTraverser.cpp RecursiveASTVisitorTests/DeclRefExpr.cpp RecursiveASTVisitorTests/ImplicitCtor.cpp Added: cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/CXXMethodDecl.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/CXXMethodDecl.cpp?rev=368402=auto == --- cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/CXXMethodDecl.cpp (added) +++ cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/CXXMethodDecl.cpp Fri Aug 9 00:30:28 2019 @@ -0,0 +1,58 @@ +//=-- unittest/Tooling/RecursiveASTVisitorTests/CXXMethodDecl.cpp --=// +// +// 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 "TestVisitor.h" +#include "clang/AST/Expr.h" + +using namespace clang; + +namespace { + +class CXXMethodDeclVisitor +: public ExpectedLocationVisitor { +public: + CXXMethodDeclVisitor(bool VisitImplicitCode) + : VisitImplicitCode(VisitImplicitCode) {} + + bool shouldVisitImplicitCode() const { return VisitImplicitCode; } + + bool VisitDeclRefExpr(DeclRefExpr *D) { +Match("declref", D->getLocation()); +return true; + } + bool VisitParmVarDecl(ParmVarDecl *P) { +Match("parm", P->getLocation()); +return true; + } + +private: + bool VisitImplicitCode; +}; + +TEST(RecursiveASTVisitor, CXXMethodDeclNoDefaultBodyVisited) { + for (bool VisitImplCode : {false, true}) { +CXXMethodDeclVisitor Visitor(VisitImplCode); +if (VisitImplCode) + Visitor.ExpectMatch("declref", 8, 28); +else + Visitor.DisallowMatch("declref", 8, 28); + +Visitor.ExpectMatch("parm", 8, 27); +llvm::StringRef Code = R"cpp( + struct B {}; + struct A { +B BB; +A =(A &); + }; + + A ::operator=(A &) = default; +)cpp"; +EXPECT_TRUE(Visitor.runOver(Code, CXXMethodDeclVisitor::Lang_CXX11)); + } +} +} // end anonymous namespace ___ cfe-commits mailing list cfe-commits@lists.llvm.org
[clang-tools-extra] r368291 - [clangd] Added semantic highlighting support for primitives.
Author: jvikstrom Date: Thu Aug 8 06:10:30 2019 New Revision: 368291 URL: http://llvm.org/viewvc/llvm-project?rev=368291=rev Log: [clangd] Added semantic highlighting support for primitives. Summary: Adds a new HighlightingKind "Primitive". Adds a special case for TypeLocs that have an underlying TypePtr that is are builtin types, adding them as primitives. The primary reason for this change is because otherwise typedefs that typedef primitives `typedef int A` would not get highlighted (so in the example `A` would not get any highlightings.) Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65943 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/SemanticHighlighting.h clang-tools-extra/trunk/clangd/test/semantic-highlighting.test clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=368291=368290=368291=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Thu Aug 8 06:10:30 2019 @@ -138,9 +138,13 @@ public: private: void addTypeLoc(SourceLocation Loc, const TypeLoc ) { -if (const Type *TP = TL.getTypePtr()) +if (const Type *TP = TL.getTypePtr()) { if (const TagDecl *TD = TP->getAsTagDecl()) addToken(Loc, TD); + if (TP->isBuiltinType()) +// Builtins must be special cased as they do not have a TagDecl. +addToken(Loc, HighlightingKind::Primitive); +} } void addToken(SourceLocation Loc, const NamedDecl *D) { @@ -386,6 +390,8 @@ llvm::StringRef toTextMateScope(Highligh return "entity.name.namespace.cpp"; case HighlightingKind::TemplateParameter: return "entity.name.type.template.cpp"; + case HighlightingKind::Primitive: +return "storage.type.primitive.cpp"; case HighlightingKind::NumKinds: llvm_unreachable("must not pass NumKinds to the function"); } Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=368291=368290=368291=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Thu Aug 8 06:10:30 2019 @@ -33,6 +33,7 @@ enum class HighlightingKind { EnumConstant, Namespace, TemplateParameter, + Primitive, NumKinds, }; Modified: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/test/semantic-highlighting.test?rev=368291=368290=368291=diff == --- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test (original) +++ clang-tools-extra/trunk/clangd/test/semantic-highlighting.test Thu Aug 8 06:10:30 2019 @@ -30,6 +30,9 @@ # CHECK-NEXT: ], # CHECK-NEXT: [ # CHECK-NEXT:"entity.name.type.template.cpp" +# CHECK-NEXT: ], +# CHECK-NEXT: [ +# CHECK-NEXT:"storage.type.primitive.cpp" # CHECK-NEXT: ] # CHECK-NEXT:] # CHECK-NEXT: }, @@ -40,7 +43,7 @@ # CHECK-NEXT:"lines": [ # CHECK-NEXT: { # CHECK-NEXT:"line": 0, -# CHECK-NEXT:"tokens": "BAABAAA=" +# CHECK-NEXT:"tokens": "AAADAAkEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT:], # CHECK-NEXT:"textDocument": { @@ -55,11 +58,11 @@ # CHECK-NEXT:"lines": [ # CHECK-NEXT: { # CHECK-NEXT:"line": 0, -# CHECK-NEXT:"tokens": "BAABAAA=" +# CHECK-NEXT:"tokens": "AAADAAkEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT: { # CHECK-NEXT:"line": 1, -# CHECK-NEXT:"tokens": "BAABAAA=" +# CHECK-NEXT:"tokens": "AAADAAkEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT:], # CHECK-NEXT:"textDocument": { @@ -74,7 +77,7 @@ # CHECK-NEXT:"lines": [ # CHECK-NEXT: { # CHECK-NEXT:"line": 1, -# CHECK-NEXT:"tokens": "BAABAAA=" +# CHECK-NEXT:"tokens": "AAADAAkEAAEAAA==" # CHECK-NEXT: } # CHECK-NEXT: ], # CHECK-NEXT:"textDocument": { Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=368291=368290=368291=diff
[clang-tools-extra] r368287 - [clangd] Added an early return from VisitMemberExpr in SemanticHighlighting if underlying MemberDecl is a CXXConversionDecl.
Author: jvikstrom Date: Thu Aug 8 05:43:55 2019 New Revision: 368287 URL: http://llvm.org/viewvc/llvm-project?rev=368287=rev Log: [clangd] Added an early return from VisitMemberExpr in SemanticHighlighting if underlying MemberDecl is a CXXConversionDecl. Summary: Conversion operators contain invalid MemberLocs which caused SemanticHighlighting to emit a lot of error logs in large files as they can occur fairly often (for example converting StringRef to std string). As the only thing happening was a lot of error logs being emited there doesn't really seem to be any way to test this (no erroneous tokens are added). But emiting as many logs as were being emited is not wanted. This also adds a test to guard against regressions for highlightings disapearing from places where the conversion operators are used as their behaviour differ from the other CXXMethodDecls. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65928 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=368287=368286=368287=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Thu Aug 8 05:43:55 2019 @@ -52,6 +52,10 @@ public: // When calling the destructor manually like: AAA::~A(); The ~ is a // MemberExpr. Other methods should still be highlighted though. return true; +if (isa(MD)) + // The MemberLoc is invalid for C++ conversion operators. We do not + // attempt to add tokens with invalid locations. + return true; addToken(ME->getMemberLoc(), MD); return true; } Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=368287=368286=368287=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Thu Aug 8 05:43:55 2019 @@ -255,6 +255,23 @@ TEST(SemanticHighlighting, GetsCorrectTo struct $Class[[Tmpl]] {$TemplateParameter[[T]] $Field[[x]] = 0;}; extern template struct $Class[[Tmpl]]; template struct $Class[[Tmpl]]; +)cpp", +// This test is to guard against highlightings disappearing when using +// conversion operators as their behaviour in the clang AST differ from +// other CXXMethodDecls. +R"cpp( + class $Class[[Foo]] {}; + struct $Class[[Bar]] { +explicit operator $Class[[Foo]]*() const; +explicit operator int() const; +operator $Class[[Foo]](); + }; + void $Function[[f]]() { +$Class[[Bar]] $Variable[[B]]; +$Class[[Foo]] $Variable[[F]] = $Variable[[B]]; +$Class[[Foo]] *$Variable[[FP]] = ($Class[[Foo]]*)$Variable[[B]]; +int $Variable[[I]] = (int)$Variable[[B]]; + } )cpp"}; for (const auto : TestCases) { checkHighlightings(TestCase); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r368261 - [clangd] Fix implicit template instatiations appearing as topLevelDecls.
Author: jvikstrom Date: Thu Aug 8 00:21:06 2019 New Revision: 368261 URL: http://llvm.org/viewvc/llvm-project?rev=368261=rev Log: [clangd] Fix implicit template instatiations appearing as topLevelDecls. Summary: The parser gives implicit template instantiations to the action's HandleTopLevelDecls callback. This makes semantic highlighting highlight these templated functions multiple times. Fixed by filtering on if a Decl is an implicit function/variable/class instantiation. Also added a testcase to semantic highlighting on this. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65510 Modified: clang-tools-extra/trunk/clangd/AST.cpp clang-tools-extra/trunk/clangd/AST.h clang-tools-extra/trunk/clangd/ClangdUnit.cpp clang-tools-extra/trunk/clangd/CodeComplete.cpp clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/AST.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/AST.cpp?rev=368261=368260=368261=diff == --- clang-tools-extra/trunk/clangd/AST.cpp (original) +++ clang-tools-extra/trunk/clangd/AST.cpp Thu Aug 8 00:21:06 2019 @@ -15,6 +15,7 @@ #include "clang/AST/TemplateBase.h" #include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" +#include "clang/Basic/Specifiers.h" #include "clang/Index/USRGeneration.h" #include "llvm/ADT/Optional.h" #include "llvm/Support/Casting.h" @@ -41,13 +42,57 @@ getTemplateSpecializationArgLocs(const N // contain TemplateArgumentLoc information. return llvm::None; } + +template +bool isTemplateSpecializationKind(const NamedDecl *D, + TemplateSpecializationKind Kind) { + if (const auto *TD = dyn_cast(D)) +return TD->getTemplateSpecializationKind() == Kind; + return false; +} + +bool isTemplateSpecializationKind(const NamedDecl *D, + TemplateSpecializationKind Kind) { + return isTemplateSpecializationKind(D, Kind) || + isTemplateSpecializationKind(D, Kind) || + isTemplateSpecializationKind(D, Kind); +} + } // namespace +bool isImplicitTemplateInstantiation(const NamedDecl *D) { + return isTemplateSpecializationKind(D, TSK_ImplicitInstantiation); +} + +bool isExplicitTemplateSpecialization(const NamedDecl *D) { + return isTemplateSpecializationKind(D, TSK_ExplicitSpecialization); +} + bool isImplementationDetail(const Decl *D) { return !isSpelledInSource(D->getLocation(), D->getASTContext().getSourceManager()); } +// Returns true if the complete name of decl \p D is spelled in the source code. +// This is not the case for: +// * symbols formed via macro concatenation, the spelling location will +// be "" +// * symbols controlled and defined by a compile command-line option +// `-DName=foo`, the spelling location will be "". +bool isSpelledInSourceCode(const Decl *D) { + const auto = D->getASTContext().getSourceManager(); + auto Loc = D->getLocation(); + // FIXME: Revisit the strategy, the heuristic is limitted when handling + // macros, we should use the location where the whole definition occurs. + if (Loc.isMacroID()) { +std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM); +if (llvm::StringRef(PrintLoc).startswith("")) + return false; + } + return true; +} + SourceLocation findName(const clang::Decl *D) { return D->getLocation(); } Modified: clang-tools-extra/trunk/clangd/AST.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/AST.h?rev=368261=368260=368261=diff == --- clang-tools-extra/trunk/clangd/AST.h (original) +++ clang-tools-extra/trunk/clangd/AST.h Thu Aug 8 00:21:06 2019 @@ -80,9 +80,23 @@ std::string printType(const QualType QT, /// take in to account using directives etc /// Example: shortenNamespace("ns1::MyClass", "ns1") ///--> "MyClass" -std::string shortenNamespace(const llvm::StringRef OriginalName, - const llvm::StringRef CurrentNamespace); +std::string shortenNamespace(const llvm::StringRef OriginalName, + const llvm::StringRef CurrentNamespace); +/// Indicates if \p D is a template instantiation implicitly generated by the +/// compiler, e.g. +/// template struct vector {}; +/// vector v; // 'vector' is an implicit instantiation +bool isImplicitTemplateInstantiation(const NamedDecl *D); +/// Indicates if \p D is an explicit template specialization, e.g. +/// template struct vector {}; +/// template <> struct vector {}; // <-- explicit specialization +/// +/// Note that explicit instantiations are NOT
[clang-tools-extra] r368136 - [clangd] Added a TextMate theme parser to the vscode extension.
Author: jvikstrom Date: Wed Aug 7 01:48:52 2019 New Revision: 368136 URL: http://llvm.org/viewvc/llvm-project?rev=368136=rev Log: [clangd] Added a TextMate theme parser to the vscode extension. Summary: Adds a TextMate parser module to the vscode extension. It parses a theme into an array of a pair of TextMate scopes and text colors. Reviewers: hokein, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65738 Added: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/assets/ clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/assets/includeTheme.jsonc clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/assets/simpleTheme.jsonc clang-tools-extra/trunk/clangd/clients/clangd-vscode/test/semantic-highlighting.test.ts Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json Modified: clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json?rev=368136=368135=368136=diff == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json (original) +++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/package.json Wed Aug 7 01:48:52 2019 @@ -36,14 +36,15 @@ "test": "node ./node_modules/vscode/bin/test" }, "dependencies": { +"jsonc-parser": "^2.1.0", "vscode-languageclient": "^5.3.0-next.6", "vscode-languageserver": "^5.3.0-next.6" }, "devDependencies": { "@types/mocha": "^2.2.32", "@types/node": "^6.0.40", -"mocha": "^5.2.0", "clang-format": "1.2.4", +"mocha": "^5.2.0", "typescript": "^2.0.3", "vscode": "^1.1.0" }, Added: clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts?rev=368136=auto == --- clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts (added) +++ clang-tools-extra/trunk/clangd/clients/clangd-vscode/src/semantic-highlighting.ts Wed Aug 7 01:48:52 2019 @@ -0,0 +1,102 @@ +import * as fs from 'fs'; +import * as jsonc from "jsonc-parser"; +import * as path from 'path'; +import * as vscode from 'vscode'; + +// A rule for how to color TextMate scopes. +interface TokenColorRule { + // A TextMate scope that specifies the context of the token, e.g. + // "entity.name.function.cpp". + scope: string; + // foreground is the color tokens of this scope should have. + foreground: string; +} + +// Get all token color rules provided by the theme. +function loadTheme(themeName: string): Promise { + const extension = + vscode.extensions.all.find((extension: vscode.Extension) => { +const contribs = extension.packageJSON.contributes; +if (!contribs || !contribs.themes) + return false; +return contribs.themes.some((theme: any) => theme.id === themeName || +theme.label === themeName); + }); + + if (!extension) { +return Promise.reject('Could not find a theme with name: ' + themeName); + } + + const themeInfo = extension.packageJSON.contributes.themes.find( + (theme: any) => theme.id === themeName || theme.label === themeName); + return parseThemeFile(path.join(extension.extensionPath, themeInfo.path)); +} + +/** + * Parse the TextMate theme at fullPath. If there are multiple TextMate scopes + * of the same name in the include chain only the earliest entry of the scope is + * saved. + * @param fullPath The absolute path to the theme. + * @param seenScopes A set containing the name of the scopes that have already + * been set. + */ +export async function parseThemeFile( +fullPath: string, seenScopes?: Set): Promise { + if (!seenScopes) +seenScopes = new Set(); + // FIXME: Add support for themes written as .tmTheme. + if (path.extname(fullPath) === '.tmTheme') +return []; + try { +const contents = await readFileText(fullPath); +const parsed = jsonc.parse(contents); +const rules: TokenColorRule[] = []; +// To make sure it does not crash if tokenColors is undefined. +if (!parsed.tokenColors) + parsed.tokenColors = []; +parsed.tokenColors.forEach((rule: any) => { + if (!rule.scope || !rule.settings || !rule.settings.foreground) +return; + const textColor = rule.settings.foreground; + // Scopes that were found further up the TextMate chain should not be + // overwritten. + const addColor = (scope: string) => { +
r367892 - [AST] Fix buildbot failure because of raw string inside macro from 367839.
Author: jvikstrom Date: Mon Aug 5 10:14:46 2019 New Revision: 367892 URL: http://llvm.org/viewvc/llvm-project?rev=367892=rev Log: [AST] Fix buildbot failure because of raw string inside macro from 367839. Modified: cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp Modified: cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp?rev=367892=367891=367892=diff == --- cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp (original) +++ cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp Mon Aug 5 10:14:46 2019 @@ -41,7 +41,7 @@ TEST(RecursiveASTVisitor, CXXCtorInitial for (bool VisitImplCode : {true, false}) { CXXCtorInitializerVisitor Visitor(VisitImplCode); Visitor.ExpectMatch("initializer", 7, 17); -auto Code = R"cpp( +llvm::StringRef Code = R"cpp( class A {}; class B : public A { B() {}; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
r367839 - [AST] Fix RecursiveASTVisitor visiting implicit constructor initializers.
Author: jvikstrom Date: Mon Aug 5 05:20:43 2019 New Revision: 367839 URL: http://llvm.org/viewvc/llvm-project?rev=367839=rev Log: [AST] Fix RecursiveASTVisitor visiting implicit constructor initializers. Summary: RecursiveASTVisitor was visiting implcit constructor initializers. This caused semantic highlighting in clangd to emit error logs. Fixes this by checking if the constructor is written or if the visitor should visit implicit decls. Reviewers: hokein, ilya-biryukov Subscribers: kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65735 Added: cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp Modified: cfe/trunk/include/clang/AST/RecursiveASTVisitor.h cfe/trunk/unittests/Tooling/CMakeLists.txt Modified: cfe/trunk/include/clang/AST/RecursiveASTVisitor.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/RecursiveASTVisitor.h?rev=367839=367838=367839=diff == --- cfe/trunk/include/clang/AST/RecursiveASTVisitor.h (original) +++ cfe/trunk/include/clang/AST/RecursiveASTVisitor.h Mon Aug 5 05:20:43 2019 @@ -2023,7 +2023,8 @@ bool RecursiveASTVisitor::Trave if (CXXConstructorDecl *Ctor = dyn_cast(D)) { // Constructor initializers. for (auto *I : Ctor->inits()) { - TRY_TO(TraverseConstructorInitializer(I)); + if (I->isWritten() || getDerived().shouldVisitImplicitCode()) +TRY_TO(TraverseConstructorInitializer(I)); } } Modified: cfe/trunk/unittests/Tooling/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/CMakeLists.txt?rev=367839=367838=367839=diff == --- cfe/trunk/unittests/Tooling/CMakeLists.txt (original) +++ cfe/trunk/unittests/Tooling/CMakeLists.txt Mon Aug 5 05:20:43 2019 @@ -31,6 +31,7 @@ add_clang_unittest(ToolingTests RecursiveASTVisitorTests/CXXOperatorCallExprTraverser.cpp RecursiveASTVisitorTests/DeclRefExpr.cpp RecursiveASTVisitorTests/ImplicitCtor.cpp + RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp RecursiveASTVisitorTests/InitListExprPostOrder.cpp RecursiveASTVisitorTests/InitListExprPostOrderNoQueue.cpp RecursiveASTVisitorTests/InitListExprPreOrder.cpp Added: cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp?rev=367839=auto == --- cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp (added) +++ cfe/trunk/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp Mon Aug 5 05:20:43 2019 @@ -0,0 +1,57 @@ +//=- unittest/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp -=// +// +// 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 "TestVisitor.h" + +using namespace clang; + +namespace { + +class CXXCtorInitializerVisitor +: public ExpectedLocationVisitor { +public: + CXXCtorInitializerVisitor(bool VisitImplicitCode) + : VisitImplicitCode(VisitImplicitCode) {} + + bool shouldVisitImplicitCode() const { return VisitImplicitCode; } + + bool TraverseConstructorInitializer(CXXCtorInitializer *Init) { +if (!Init->isWritten()) + VisitedImplicitInitializer = true; +Match("initializer", Init->getSourceLocation()); +return ExpectedLocationVisitor< +CXXCtorInitializerVisitor>::TraverseConstructorInitializer(Init); + } + + bool VisitedImplicitInitializer = false; + +private: + bool VisitImplicitCode; +}; + +// Check to ensure that CXXCtorInitializer is not visited when implicit code +// should not be visited and that it is visited when implicit code should be +// visited. +TEST(RecursiveASTVisitor, CXXCtorInitializerVisitNoImplicit) { + for (bool VisitImplCode : {true, false}) { +CXXCtorInitializerVisitor Visitor(VisitImplCode); +Visitor.ExpectMatch("initializer", 7, 17); +EXPECT_TRUE(Visitor.runOver(R"cpp( +class A {}; +class B : public A { + B() {}; +}; +class C : public A { + C() : A() {} +}; + )cpp", +CXXCtorInitializerVisitor::Lang_CXX)); +EXPECT_EQ(Visitor.VisitedImplicitInitializer, VisitImplCode); + } +} +} // end anonymous namespace ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r367529 - [clangd] Fix buildbot failure from ambigous ArrayRef ctor
Author: jvikstrom Date: Thu Aug 1 02:08:41 2019 New Revision: 367529 URL: http://llvm.org/viewvc/llvm-project?rev=367529=rev Log: [clangd] Fix buildbot failure from ambigous ArrayRef ctor Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=367529=367528=367529=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Thu Aug 1 02:08:41 2019 @@ -292,9 +292,9 @@ diffHighlightings(ArrayRef DiffedLines; // ArrayRefs to the current line in the highlightings. ArrayRef NewLine(New.begin(), - /*length*/0UL); + /*length*/ static_cast(0)); ArrayRef OldLine(Old.begin(), - /*length*/ 0UL); + /*length*/ static_cast(0)); auto NewEnd = New.end(); auto OldEnd = Old.end(); auto NextLineNumber = [&]() { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r367521 - [clangd] Duplicate lines of semantic highlightings sent removed.
Author: jvikstrom Date: Thu Aug 1 01:08:44 2019 New Revision: 367521 URL: http://llvm.org/viewvc/llvm-project?rev=367521=rev Log: [clangd] Duplicate lines of semantic highlightings sent removed. Summary: Added a class for diffing highlightings and removing duplicate lines. Integrated into the highlighting generation flow. Only works correctly if all tokens are on a single line. Also returns empty lines if the IDE should remove previous highlightings on a line. Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64475 Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp clang-tools-extra/trunk/clangd/ClangdLSPServer.h clang-tools-extra/trunk/clangd/ClangdServer.cpp clang-tools-extra/trunk/clangd/ClangdServer.h clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/SemanticHighlighting.h clang-tools-extra/trunk/clangd/test/semantic-highlighting.test clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp?rev=367521=367520=367521=diff == --- clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp Thu Aug 1 01:08:44 2019 @@ -615,6 +615,10 @@ void ClangdLSPServer::onDocumentDidClose std::lock_guard Lock(FixItsMutex); FixItsMap.erase(File); } + { +std::lock_guard HLock(HighlightingsMutex); +FileToHighlightings.erase(File); + } // clangd will not send updates for this file anymore, so we empty out the // list of diagnostics shown on the client (e.g. in the "Problems" pane of // VSCode). Note that this cannot race with actual diagnostics responses @@ -1113,10 +1117,21 @@ bool ClangdLSPServer::shouldRunCompletio } void ClangdLSPServer::onHighlightingsReady( -PathRef File, std::vector Highlightings) { +PathRef File, std::vector Highlightings, int NumLines) { + std::vector Old; + std::vector HighlightingsCopy = Highlightings; + { +std::lock_guard Lock(HighlightingsMutex); +Old = std::move(FileToHighlightings[File]); +FileToHighlightings[File] = std::move(HighlightingsCopy); + } + // LSP allows us to send incremental edits of highlightings. Also need to diff + // to remove highlightings from tokens that should no longer have them. + std::vector Diffed = + diffHighlightings(Highlightings, Old, NumLines); publishSemanticHighlighting( {{URIForFile::canonicalize(File, /*TUPath=*/File)}, - toSemanticHighlightingInformation(Highlightings)}); + toSemanticHighlightingInformation(Diffed)}); } void ClangdLSPServer::onDiagnosticsReady(PathRef File, Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.h?rev=367521=367520=367521=diff == --- clang-tools-extra/trunk/clangd/ClangdLSPServer.h (original) +++ clang-tools-extra/trunk/clangd/ClangdLSPServer.h Thu Aug 1 01:08:44 2019 @@ -55,9 +55,9 @@ private: // Implement DiagnosticsConsumer. void onDiagnosticsReady(PathRef File, std::vector Diagnostics) override; void onFileUpdated(PathRef File, const TUStatus ) override; - void - onHighlightingsReady(PathRef File, - std::vector Highlightings) override; + void onHighlightingsReady(PathRef File, +std::vector Highlightings, +int NumLines) override; // LSP methods. Notifications have signature void(const Params&). // Calls have signature void(const Params&, Callback). @@ -138,6 +138,8 @@ private: DiagnosticToReplacementMap; /// Caches FixIts per file and diagnostics llvm::StringMap FixItsMap; + std::mutex HighlightingsMutex; + llvm::StringMap> FileToHighlightings; // Most code should not deal with Transport directly. // MessageHandler deals with incoming messages, use call() etc for outgoing. Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=367521=367520=367521=diff == --- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Thu Aug 1 01:08:44 2019 @@ -71,10 +71,19 @@ struct UpdateIndexCallbacks : public Par if (SemanticHighlighting) Highlightings = getSemanticHighlightings(AST); +// FIXME: We need a better way to send the maximum line number to the +// differ. +// The differ needs the
[clang-tools-extra] r366420 - [clangd] Added highlightings for template parameters and specializations.
Author: jvikstrom Date: Thu Jul 18 02:56:38 2019 New Revision: 366420 URL: http://llvm.org/viewvc/llvm-project?rev=366420=rev Log: [clangd] Added highlightings for template parameters and specializations. Summary: Template parameters and specializations were not being highlighted before. This adds highlightings to those types of tokens by adding two Visit* methods. Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64855 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/SemanticHighlighting.h clang-tools-extra/trunk/clangd/test/semantic-highlighting.test clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=366420=366419=366420=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Thu Jul 18 02:56:38 2019 @@ -99,6 +99,19 @@ public: return true; } + bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc ) { +// TemplateTypeParmTypeLoc does not have a TagDecl in its type ptr. +addToken(TL.getBeginLoc(), TL.getDecl()); +return true; + } + + bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc ) { +if (const TemplateDecl *TD = +TL.getTypePtr()->getTemplateName().getAsTemplateDecl()) + addToken(TL.getBeginLoc(), TD); +return true; + } + bool VisitTypeLoc(TypeLoc ) { // This check is for not getting two entries when there are anonymous // structs. It also makes us not highlight certain namespace qualifiers @@ -135,6 +148,10 @@ private: // We highlight class decls, constructor decls and destructor decls as // `Class` type. The destructor decls are handled in `VisitTypeLoc` (we will // visit a TypeLoc where the underlying Type is a CXXRecordDecl). +if (isa(D)) { + addToken(Loc, HighlightingKind::Class); + return; +} if (isa(D)) { addToken(Loc, HighlightingKind::Class); return; @@ -175,6 +192,14 @@ private: addToken(Loc, HighlightingKind::Namespace); return; } +if (isa(D)) { + addToken(Loc, HighlightingKind::TemplateParameter); + return; +} +if (isa(D)) { + addToken(Loc, HighlightingKind::TemplateParameter); + return; +} } void addToken(SourceLocation Loc, HighlightingKind Kind) { @@ -297,6 +322,8 @@ llvm::StringRef toTextMateScope(Highligh return "variable.other.enummember.cpp"; case HighlightingKind::Namespace: return "entity.name.namespace.cpp"; + case HighlightingKind::TemplateParameter: +return "entity.name.type.template.cpp"; case HighlightingKind::NumKinds: llvm_unreachable("must not pass NumKinds to the function"); } Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=366420=366419=366420=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Thu Jul 18 02:56:38 2019 @@ -32,6 +32,7 @@ enum class HighlightingKind { Enum, EnumConstant, Namespace, + TemplateParameter, NumKinds, }; Modified: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/test/semantic-highlighting.test?rev=366420=366419=366420=diff == --- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test (original) +++ clang-tools-extra/trunk/clangd/test/semantic-highlighting.test Thu Jul 18 02:56:38 2019 @@ -27,6 +27,9 @@ # CHECK-NEXT: ], # CHECK-NEXT: [ # CHECK-NEXT:"entity.name.namespace.cpp" +# CHECK-NEXT: ], +# CHECK-NEXT: [ +# CHECK-NEXT:"entity.name.type.template.cpp" # CHECK-NEXT: ] # CHECK-NEXT:] # CHECK-NEXT: }, Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=366420=366419=366420=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Thu Jul 18 02:56:38 2019 @@ -40,7 +40,8 @@ void checkHighlightings(llvm::StringRef
[clang-tools-extra] r366207 - [clangd] Added highlighting for the targets in typedefs and using.
Author: jvikstrom Date: Tue Jul 16 06:23:12 2019 New Revision: 366207 URL: http://llvm.org/viewvc/llvm-project?rev=366207=rev Log: [clangd] Added highlighting for the targets in typedefs and using. Summary: In `typedef int A` the `A` was not highlighted previously. This patch gives `A` the same kind of highlighting that the underlying type has (class/enum) (which in this example is no special highlighting because builtins are not handled yet) Will add highlightings for built ins in another patch. Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64754 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=366207=366206=366207=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Tue Jul 16 06:23:12 2019 @@ -93,6 +93,12 @@ public: return true; } + bool VisitTypedefNameDecl(TypedefNameDecl *TD) { +if(const auto *TSI = TD->getTypeSourceInfo()) + addTypeLoc(TD->getLocation(), TSI->getTypeLoc()); +return true; + } + bool VisitTypeLoc(TypeLoc ) { // This check is for not getting two entries when there are anonymous // structs. It also makes us not highlight certain namespace qualifiers @@ -101,9 +107,7 @@ public: if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated) return true; -if (const Type *TP = TL.getTypePtr()) - if (const TagDecl *TD = TP->getAsTagDecl()) -addToken(TL.getBeginLoc(), TD); +addTypeLoc(TL.getBeginLoc(), TL); return true; } @@ -118,6 +122,12 @@ public: } private: + void addTypeLoc(SourceLocation Loc, const TypeLoc ) { +if (const Type *TP = TL.getTypePtr()) + if (const TagDecl *TD = TP->getAsTagDecl()) +addToken(Loc, TD); + } + void addToken(SourceLocation Loc, const NamedDecl *D) { if (D->getDeclName().isIdentifier() && D->getName().empty()) // Don't add symbols that don't have any length. Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=366207=366206=366207=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Tue Jul 16 06:23:12 2019 @@ -90,7 +90,7 @@ TEST(SemanticHighlighting, GetsCorrectTo typename T::A* $Field[[D]]; }; $Namespace[[abc]]::$Class[[A]] $Variable[[AA]]; - typedef $Namespace[[abc]]::$Class[[A]] AAA; + typedef $Namespace[[abc]]::$Class[[A]] $Class[[AAA]]; struct $Class[[B]] { $Class[[B]](); ~$Class[[B]](); @@ -173,6 +173,19 @@ TEST(SemanticHighlighting, GetsCorrectTo } int $Variable[[B]]; $Class[[AA]] $Variable[[A]]{$Variable[[B]]}; +)cpp", +R"cpp( + namespace $Namespace[[a]] { +struct $Class[[A]] {}; + } + typedef $Namespace[[a]]::$Class[[A]] $Class[[B]]; + using $Class[[BB]] = $Namespace[[a]]::$Class[[A]]; + enum class $Enum[[E]] {}; + typedef $Enum[[E]] $Enum[[C]]; + typedef $Enum[[C]] $Enum[[CC]]; + using $Enum[[CD]] = $Enum[[CC]]; + $Enum[[CC]] $Function[[f]]($Class[[B]]); + $Enum[[CD]] $Function[[f]]($Class[[BB]]); )cpp"}; for (const auto : TestCases) { checkHighlightings(TestCase); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r366070 - [clangd] Fix duplicate highlighting tokens appearing in initializer lists.
Author: jvikstrom Date: Mon Jul 15 08:08:27 2019 New Revision: 366070 URL: http://llvm.org/viewvc/llvm-project?rev=366070=rev Log: [clangd] Fix duplicate highlighting tokens appearing in initializer lists. Summary: The RecursiveASTVisitor sometimes visits exprs in initializer lists twice. Added deduplication to prevent duplicate highlighting tokens from appearing. Done by sorting and a linear search. Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64634 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=366070=366069=366070=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Mon Jul 15 08:08:27 2019 @@ -31,6 +31,14 @@ public: std::vector collectTokens() { Tokens.clear(); TraverseAST(Ctx); +// Initializer lists can give duplicates of tokens, therefore all tokens +// must be deduplicated. +llvm::sort(Tokens, + [](const HighlightingToken , const HighlightingToken ) { + return std::tie(L.R, L.Kind) < std::tie(R.R, R.Kind); + }); +auto Last = std::unique(Tokens.begin(), Tokens.end()); +Tokens.erase(Last, Tokens.end()); return Tokens; } Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=366070=366069=366070=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Mon Jul 15 08:08:27 2019 @@ -166,6 +166,13 @@ TEST(SemanticHighlighting, GetsCorrectTo $Variable[[AA]].$Field[[E]].$Field[[C]]; $Class[[A]]::$Variable[[S]] = 90; } +)cpp", +R"cpp( + struct $Class[[AA]] { +int $Field[[A]]; + } + int $Variable[[B]]; + $Class[[AA]] $Variable[[A]]{$Variable[[B]]}; )cpp"}; for (const auto : TestCases) { checkHighlightings(TestCase); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r366047 - [clangd] Added highlighting for members and methods.
Author: jvikstrom Date: Mon Jul 15 01:12:21 2019 New Revision: 366047 URL: http://llvm.org/viewvc/llvm-project?rev=366047=rev Log: [clangd] Added highlighting for members and methods. Summary: Added highlighting for members and methods. Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64617 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/SemanticHighlighting.h clang-tools-extra/trunk/clangd/test/semantic-highlighting.test clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=366047=366046=366047=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Mon Jul 15 01:12:21 2019 @@ -40,6 +40,16 @@ public: return true; } + bool VisitMemberExpr(MemberExpr *ME) { +const auto *MD = ME->getMemberDecl(); +if (isa(MD)) + // When calling the destructor manually like: AAA::~A(); The ~ is a + // MemberExpr. Other methods should still be highlighted though. + return true; +addToken(ME->getMemberLoc(), MD); +return true; + } + bool VisitNamedDecl(NamedDecl *ND) { // UsingDirectiveDecl's namespaces do not show up anywhere else in the // Visit/Traverse mehods. But they should also be highlighted as a @@ -115,6 +125,14 @@ private: addToken(Loc, HighlightingKind::Class); return; } +if (isa(D)) { + addToken(Loc, HighlightingKind::Method); + return; +} +if (isa(D)) { + addToken(Loc, HighlightingKind::Field); + return; +} if (isa(D)) { addToken(Loc, HighlightingKind::Enum); return; @@ -247,8 +265,12 @@ llvm::StringRef toTextMateScope(Highligh switch (Kind) { case HighlightingKind::Function: return "entity.name.function.cpp"; + case HighlightingKind::Method: +return "entity.name.function.method.cpp"; case HighlightingKind::Variable: -return "variable.cpp"; +return "variable.other.cpp"; + case HighlightingKind::Field: +return "variable.other.field.cpp"; case HighlightingKind::Class: return "entity.name.type.class.cpp"; case HighlightingKind::Enum: Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=366047=366046=366047=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Mon Jul 15 01:12:21 2019 @@ -26,6 +26,8 @@ namespace clangd { enum class HighlightingKind { Variable = 0, Function, + Method, + Field, Class, Enum, EnumConstant, Modified: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/test/semantic-highlighting.test?rev=366047=366046=366047=diff == --- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test (original) +++ clang-tools-extra/trunk/clangd/test/semantic-highlighting.test Mon Jul 15 01:12:21 2019 @@ -5,12 +5,18 @@ # CHECK: "semanticHighlighting": { # CHECK-NEXT:"scopes": [ # CHECK-NEXT: [ -# CHECK-NEXT:"variable.cpp" +# CHECK-NEXT:"variable.other.cpp" # CHECK-NEXT: ], # CHECK-NEXT: [ # CHECK-NEXT:"entity.name.function.cpp" # CHECK-NEXT: ], # CHECK-NEXT: [ +# CHECK-NEXT:"entity.name.function.method.cpp" +# CHECK-NEXT: ], +# CHECK-NEXT: [ +# CHECK-NEXT:"variable.other.field.cpp" +# CHECK-NEXT: ], +# CHECK-NEXT: [ # CHECK-NEXT:"entity.name.type.class.cpp" # CHECK-NEXT: ], # CHECK-NEXT: [ Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=366047=366046=366047=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Mon Jul 15 01:12:21 2019 @@ -38,7 +38,9 @@ void checkHighlightings(llvm::StringRef {HighlightingKind::Class, "Class"}, {HighlightingKind::Enum, "Enum"}, {HighlightingKind::Namespace, "Namespace"}, -
[clang-tools-extra] r366045 - [clangd] Added highlighting to enum constants.
Author: jvikstrom Date: Mon Jul 15 00:41:12 2019 New Revision: 366045 URL: http://llvm.org/viewvc/llvm-project?rev=366045=rev Log: [clangd] Added highlighting to enum constants. Summary: VSCode does not have a scope for enum constants. So they were placed under "constant.other.enum" as that seems to be the most correct scope for enum constants. However, this makes theia color them blue (the same color it uses for keywords). Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64624 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/SemanticHighlighting.h clang-tools-extra/trunk/clangd/test/semantic-highlighting.test clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=366045=366044=366045=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Mon Jul 15 00:41:12 2019 @@ -119,6 +119,10 @@ private: addToken(Loc, HighlightingKind::Enum); return; } +if (isa(D)) { + addToken(Loc, HighlightingKind::EnumConstant); + return; +} if (isa(D)) { addToken(Loc, HighlightingKind::Variable); return; @@ -249,6 +253,8 @@ llvm::StringRef toTextMateScope(Highligh return "entity.name.type.class.cpp"; case HighlightingKind::Enum: return "entity.name.type.enum.cpp"; + case HighlightingKind::EnumConstant: +return "variable.other.enummember.cpp"; case HighlightingKind::Namespace: return "entity.name.namespace.cpp"; case HighlightingKind::NumKinds: Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=366045=366044=366045=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Mon Jul 15 00:41:12 2019 @@ -28,6 +28,7 @@ enum class HighlightingKind { Function, Class, Enum, + EnumConstant, Namespace, NumKinds, Modified: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/test/semantic-highlighting.test?rev=366045=366044=366045=diff == --- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test (original) +++ clang-tools-extra/trunk/clangd/test/semantic-highlighting.test Mon Jul 15 00:41:12 2019 @@ -17,6 +17,9 @@ # CHECK-NEXT:"entity.name.type.enum.cpp" # CHECK-NEXT: ], # CHECK-NEXT: [ +# CHECK-NEXT:"variable.other.enummember.cpp" +# CHECK-NEXT: ], +# CHECK-NEXT: [ # CHECK-NEXT:"entity.name.namespace.cpp" # CHECK-NEXT: ] # CHECK-NEXT:] Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=366045=366044=366045=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Mon Jul 15 00:41:12 2019 @@ -37,7 +37,8 @@ void checkHighlightings(llvm::StringRef {HighlightingKind::Function, "Function"}, {HighlightingKind::Class, "Class"}, {HighlightingKind::Enum, "Enum"}, - {HighlightingKind::Namespace, "Namespace"}}; + {HighlightingKind::Namespace, "Namespace"}, + {HighlightingKind::EnumConstant, "EnumConstant"}}; std::vector ExpectedTokens; for (const auto : KindToString) { std::vector Toks = makeHighlightingTokens( @@ -103,12 +104,19 @@ TEST(SemanticHighlighting, GetsCorrectTo } )cpp", R"cpp( - enum class $Enum[[E]] {}; - enum $Enum[[EE]] {}; + enum class $Enum[[E]] { +$EnumConstant[[A]], +$EnumConstant[[B]], + }; + enum $Enum[[EE]] { +$EnumConstant[[Hi]], + }; struct $Class[[A]] { $Enum[[E]] EEE; $Enum[[EE]] ; }; + int $Variable[[I]] = $EnumConstant[[Hi]]; + $Enum[[E]] $Variable[[L]] = $Enum[[E]]::$EnumConstant[[B]]; )cpp", R"cpp( namespace $Namespace[[abc]] { @@ -118,7 +126,7 @@ TEST(SemanticHighlighting, GetsCorrectTo namespace $Namespace[[cde]] { struct $Class[[A]]
[clang-tools-extra] r365745 - [clangd] Added highlightings for namespace specifiers.
Author: jvikstrom Date: Thu Jul 11 02:29:16 2019 New Revision: 365745 URL: http://llvm.org/viewvc/llvm-project?rev=365745=rev Log: [clangd] Added highlightings for namespace specifiers. Summary: Added highlightings for namespace specifiers. Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64492 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/SemanticHighlighting.h clang-tools-extra/trunk/clangd/test/semantic-highlighting.test clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=365745=365744=365745=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Thu Jul 11 02:29:16 2019 @@ -11,8 +11,6 @@ #include "Protocol.h" #include "SourceCode.h" #include "clang/AST/ASTContext.h" -#include "clang/AST/Decl.h" -#include "clang/AST/DeclarationName.h" #include "clang/AST/RecursiveASTVisitor.h" namespace clang { @@ -36,7 +34,21 @@ public: return Tokens; } + bool VisitNamespaceAliasDecl(NamespaceAliasDecl *NAD) { +// The target namespace of an alias can not be found in any other way. +addToken(NAD->getTargetNameLoc(), HighlightingKind::Namespace); +return true; + } + bool VisitNamedDecl(NamedDecl *ND) { +// UsingDirectiveDecl's namespaces do not show up anywhere else in the +// Visit/Traverse mehods. But they should also be highlighted as a +// namespace. +if (const auto *UD = dyn_cast(ND)) { + addToken(UD->getIdentLocation(), HighlightingKind::Namespace); + return true; +} + // Constructors' TypeLoc has a TypePtr that is a FunctionProtoType. It has // no tag decl and therefore constructors must be gotten as NamedDecls // instead. @@ -65,17 +77,28 @@ public: bool VisitTypeLoc(TypeLoc ) { // This check is for not getting two entries when there are anonymous -// structs. It also makes us not highlight namespace qualifiers. For -// elaborated types the actual type is highlighted as an inner TypeLoc. +// structs. It also makes us not highlight certain namespace qualifiers +// twice. For elaborated types the actual type is highlighted as an inner +// TypeLoc. if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated) return true; if (const Type *TP = TL.getTypePtr()) if (const TagDecl *TD = TP->getAsTagDecl()) - addToken(TL.getBeginLoc(), TD); +addToken(TL.getBeginLoc(), TD); return true; } + bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLoc) { +if (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) + if (NNS->getKind() == NestedNameSpecifier::Namespace || + NNS->getKind() == NestedNameSpecifier::NamespaceAlias) +addToken(NNSLoc.getLocalBeginLoc(), HighlightingKind::Namespace); + +return RecursiveASTVisitor< +HighlightingTokenCollector>::TraverseNestedNameSpecifierLoc(NNSLoc); + } + private: void addToken(SourceLocation Loc, const NamedDecl *D) { if (D->getDeclName().isIdentifier() && D->getName().empty()) @@ -104,6 +127,14 @@ private: addToken(Loc, HighlightingKind::Function); return; } +if (isa(D)) { + addToken(Loc, HighlightingKind::Namespace); + return; +} +if (isa(D)) { + addToken(Loc, HighlightingKind::Namespace); + return; +} } void addToken(SourceLocation Loc, HighlightingKind Kind) { @@ -218,6 +249,8 @@ llvm::StringRef toTextMateScope(Highligh return "entity.name.type.class.cpp"; case HighlightingKind::Enum: return "entity.name.type.enum.cpp"; + case HighlightingKind::Namespace: +return "entity.name.namespace.cpp"; case HighlightingKind::NumKinds: llvm_unreachable("must not pass NumKinds to the function"); } Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=365745=365744=365745=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Thu Jul 11 02:29:16 2019 @@ -28,6 +28,7 @@ enum class HighlightingKind { Function, Class, Enum, + Namespace, NumKinds, }; Modified: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/test/semantic-highlighting.test?rev=365745=365744=365745=diff
[clang-tools-extra] r365602 - [clangd] Added highlighting for class and enum types.
Author: jvikstrom Date: Wed Jul 10 01:41:25 2019 New Revision: 365602 URL: http://llvm.org/viewvc/llvm-project?rev=365602=rev Log: [clangd] Added highlighting for class and enum types. Summary: Added highlighting for non-builtin types using VisitTypeLoc. Ignoring namespace qualifiers as for now. Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64257 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/SemanticHighlighting.h clang-tools-extra/trunk/clangd/test/semantic-highlighting.test clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=365602=365601=365602=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Wed Jul 10 01:41:25 2019 @@ -11,6 +11,8 @@ #include "Protocol.h" #include "SourceCode.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclarationName.h" #include "clang/AST/RecursiveASTVisitor.h" namespace clang { @@ -35,13 +37,18 @@ public: } bool VisitNamedDecl(NamedDecl *ND) { -// FIXME: (De)Constructors/operator need to be highlighted some other way. -if (ND->getDeclName().getNameKind() != DeclarationName::Identifier) +// Constructors' TypeLoc has a TypePtr that is a FunctionProtoType. It has +// no tag decl and therefore constructors must be gotten as NamedDecls +// instead. +if (ND->getDeclName().getNameKind() == +DeclarationName::CXXConstructorName) { + addToken(ND->getLocation(), ND); return true; +} -if (ND->getDeclName().isEmpty()) - // Don't add symbols that don't have any length. +if (ND->getDeclName().getNameKind() != DeclarationName::Identifier) return true; + addToken(ND->getLocation(), ND); return true; } @@ -56,8 +63,39 @@ public: return true; } + bool VisitTypeLoc(TypeLoc ) { +// This check is for not getting two entries when there are anonymous +// structs. It also makes us not highlight namespace qualifiers. For +// elaborated types the actual type is highlighted as an inner TypeLoc. +if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated) + return true; + +if (const Type *TP = TL.getTypePtr()) + if (const TagDecl *TD = TP->getAsTagDecl()) + addToken(TL.getBeginLoc(), TD); +return true; + } + private: - void addToken(SourceLocation Loc, const Decl *D) { + void addToken(SourceLocation Loc, const NamedDecl *D) { +if (D->getDeclName().isIdentifier() && D->getName().empty()) + // Don't add symbols that don't have any length. + return; +// We highlight class decls, constructor decls and destructor decls as +// `Class` type. The destructor decls are handled in `VisitTypeLoc` (we will +// visit a TypeLoc where the underlying Type is a CXXRecordDecl). +if (isa(D)) { + addToken(Loc, HighlightingKind::Class); + return; +} +if (isa(D)) { + addToken(Loc, HighlightingKind::Class); + return; +} +if (isa(D)) { + addToken(Loc, HighlightingKind::Enum); + return; +} if (isa(D)) { addToken(Loc, HighlightingKind::Variable); return; @@ -176,6 +214,10 @@ llvm::StringRef toTextMateScope(Highligh return "entity.name.function.cpp"; case HighlightingKind::Variable: return "variable.cpp"; + case HighlightingKind::Class: +return "entity.name.type.class.cpp"; + case HighlightingKind::Enum: +return "entity.name.type.enum.cpp"; case HighlightingKind::NumKinds: llvm_unreachable("must not pass NumKinds to the function"); } Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=365602=365601=365602=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Wed Jul 10 01:41:25 2019 @@ -26,6 +26,8 @@ namespace clangd { enum class HighlightingKind { Variable = 0, Function, + Class, + Enum, NumKinds, }; Modified: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/test/semantic-highlighting.test?rev=365602=365601=365602=diff == --- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test (original) +++
[clang-tools-extra] r365205 - [clangd] Added highlighting for variable references (declrefs)
Author: jvikstrom Date: Fri Jul 5 06:06:03 2019 New Revision: 365205 URL: http://llvm.org/viewvc/llvm-project?rev=365205=rev Log: [clangd] Added highlighting for variable references (declrefs) Summary: Added highlighting for variable references using VisitDeclRefExpr. Reviewers: hokein, sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64199 Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=365205=365204=365205=diff == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Fri Jul 5 06:06:03 2019 @@ -34,26 +34,46 @@ public: return Tokens; } - bool VisitVarDecl(VarDecl *Var) { -addToken(Var, HighlightingKind::Variable); + bool VisitNamedDecl(NamedDecl *ND) { +// FIXME: (De)Constructors/operator need to be highlighted some other way. +if (ND->getDeclName().getNameKind() != DeclarationName::Identifier) + return true; + +if (ND->getDeclName().isEmpty()) + // Don't add symbols that don't have any length. + return true; +addToken(ND->getLocation(), ND); return true; } - bool VisitFunctionDecl(FunctionDecl *Func) { -addToken(Func, HighlightingKind::Function); + + bool VisitDeclRefExpr(DeclRefExpr *Ref) { +if (Ref->getNameInfo().getName().getNameKind() != +DeclarationName::Identifier) + // Only want to highlight identifiers. + return true; + +addToken(Ref->getLocation(), Ref->getDecl()); return true; } private: - void addToken(const NamedDecl *D, HighlightingKind Kind) { -if (D->getLocation().isMacroID()) - // FIXME: skip tokens inside macros for now. + void addToken(SourceLocation Loc, const Decl *D) { +if (isa(D)) { + addToken(Loc, HighlightingKind::Variable); return; +} +if (isa(D)) { + addToken(Loc, HighlightingKind::Function); + return; +} + } -if (D->getDeclName().isEmpty()) - // Don't add symbols that don't have any length. + void addToken(SourceLocation Loc, HighlightingKind Kind) { +if (Loc.isMacroID()) + // FIXME: skip tokens inside macros for now. return; -auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation()); +auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc); if (!R) { // R should always have a value, if it doesn't something is very wrong. elog("Tried to add semantic token with an invalid range"); Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=365205=365204=365205=diff == --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Fri Jul 5 06:06:03 2019 @@ -48,20 +48,35 @@ void checkHighlightings(llvm::StringRef TEST(SemanticHighlighting, GetsCorrectTokens) { const char *TestCases[] = { - R"cpp( -struct A { - double SomeMember; -}; -struct { -} $Variable[[HStruct]]; -void $Function[[foo]](int $Variable[[a]]) { - auto $Variable[[VeryLongVariableName]] = 12312; - A $Variable[[aa]]; -} - )cpp", - R"cpp( -void $Function[[foo]](int); - )cpp"}; +R"cpp( + struct AS { +double SomeMember; + }; + struct { + } $Variable[[S]]; + void $Function[[foo]](int $Variable[[A]]) { +auto $Variable[[VeryLongVariableName]] = 12312; +AS $Variable[[AA]]; +auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]]; +auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {}; +$Variable[[FN]](12312); + } +)cpp", +R"cpp( + void $Function[[foo]](int); + void $Function[[Gah]](); + void $Function[[foo]]() { +auto $Variable[[Bou]] = $Function[[Gah]]; + } +)cpp", +R"cpp( + struct A { +A(); +~A(); +void $Function[[abc]](); +void operator<<(int); + }; +)cpp"}; for (const auto : TestCases) { checkHighlightings(TestCase); } ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r365112 - [clangd] Add missing changes for 365111
Author: jvikstrom Date: Thu Jul 4 01:06:48 2019 New Revision: 365112 URL: http://llvm.org/viewvc/llvm-project?rev=365112=rev Log: [clangd] Add missing changes for 365111 Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.h clang-tools-extra/trunk/clangd/test/semantic-highlighting.test Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.h?rev=365112=365111=365112=diff == --- clang-tools-extra/trunk/clangd/ClangdLSPServer.h (original) +++ clang-tools-extra/trunk/clangd/ClangdLSPServer.h Thu Jul 4 01:06:48 2019 @@ -55,8 +55,9 @@ private: // Implement DiagnosticsConsumer. void onDiagnosticsReady(PathRef File, std::vector Diagnostics) override; void onFileUpdated(PathRef File, const TUStatus ) override; - void onHighlightingsReady(PathRef File, - std::vector Highlightings) override; + void + onHighlightingsReady(PathRef File, + std::vector Highlightings) override; // LSP methods. Notifications have signature void(const Params&). // Calls have signature void(const Params&, Callback). Modified: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/test/semantic-highlighting.test?rev=365112=365111=365112=diff == --- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test (original) +++ clang-tools-extra/trunk/clangd/test/semantic-highlighting.test Thu Jul 4 01:06:48 2019 @@ -2,38 +2,7 @@ {"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument":{"semanticHighlightingCapabilities":{"semanticHighlighting":true}}},"trace":"off"}} --- # CHECK: "id": 0, -# CHECK-NEXT: "jsonrpc": "2.0", -# CHECK-NEXT: "result": { -# CHECK-NEXT:"capabilities": { -# CHECK-NEXT: "codeActionProvider": true, -# CHECK-NEXT: "completionProvider": { -# CHECK-NEXT:"resolveProvider": false, -# CHECK-NEXT:"triggerCharacters": [ -# CHECK-NEXT: ".", -# CHECK-NEXT: ">", -# CHECK-NEXT: ":" -# CHECK-NEXT:] -# CHECK-NEXT: }, -# CHECK-NEXT: "declarationProvider": true, -# CHECK-NEXT: "definitionProvider": true, -# CHECK-NEXT: "documentFormattingProvider": true, -# CHECK-NEXT: "documentHighlightProvider": true, -# CHECK-NEXT: "documentOnTypeFormattingProvider": { -# CHECK-NEXT:"firstTriggerCharacter": "\n", -# CHECK-NEXT:"moreTriggerCharacter": [] -# CHECK-NEXT: }, -# CHECK-NEXT: "documentRangeFormattingProvider": true, -# CHECK-NEXT: "documentSymbolProvider": true, -# CHECK-NEXT: "executeCommandProvider": { -# CHECK-NEXT:"commands": [ -# CHECK-NEXT: "clangd.applyFix", -# CHECK-NEXT: "clangd.applyTweak" -# CHECK-NEXT:] -# CHECK-NEXT: }, -# CHECK-NEXT: "hoverProvider": true, -# CHECK-NEXT: "referencesProvider": true, -# CHECK-NEXT: "renameProvider": true, -# CHECK-NEXT: "semanticHighlighting": { +# CHECK: "semanticHighlighting": { # CHECK-NEXT:"scopes": [ # CHECK-NEXT: [ # CHECK-NEXT:"variable.cpp" @@ -43,17 +12,6 @@ # CHECK-NEXT: ] # CHECK-NEXT:] # CHECK-NEXT: }, -# CHECK-NEXT: "signatureHelpProvider": { -# CHECK-NEXT:"triggerCharacters": [ -# CHECK-NEXT: "(", -# CHECK-NEXT: "," -# CHECK-NEXT:] -# CHECK-NEXT: }, -# CHECK-NEXT: "textDocumentSync": 2, -# CHECK-NEXT: "typeHierarchyProvider": true -# CHECK-NEXT: "workspaceSymbolProvider": true -# CHECK-NEXT:} -# CHECK-NEXT: } --- {"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.cpp","languageId":"cpp","version":1,"text":"int x = 2;"}}} # CHECK: "method": "textDocument/semanticHighlighting", ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r365111 - [clangd] Emit publishSemanticHighlighting in LSP if enabled
Author: jvikstrom Date: Thu Jul 4 00:53:12 2019 New Revision: 365111 URL: http://llvm.org/viewvc/llvm-project?rev=365111=rev Log: [clangd] Emit publishSemanticHighlighting in LSP if enabled Summary: Emit publishSemanticHighlighting in LSP if enabled Reviewers: hokein, kadircet Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D63919 Added: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp clang-tools-extra/trunk/clangd/ClangdLSPServer.h clang-tools-extra/trunk/clangd/Protocol.cpp clang-tools-extra/trunk/clangd/Protocol.h clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/SemanticHighlighting.h clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp?rev=365111=365110=365111=diff == --- clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp Thu Jul 4 00:53:12 2019 @@ -11,6 +11,7 @@ #include "FormattedString.h" #include "GlobalCompilationDatabase.h" #include "Protocol.h" +#include "SemanticHighlighting.h" #include "SourceCode.h" #include "Trace.h" #include "URI.h" @@ -328,6 +329,8 @@ void ClangdLSPServer::onInitialize(const WithOffsetEncoding.emplace(kCurrentOffsetEncoding, *NegotiatedOffsetEncoding); + ClangdServerOpts.SemanticHighlighting = + Params.capabilities.SemanticHighlighting; if (Params.rootUri && *Params.rootUri) ClangdServerOpts.WorkspaceRoot = Params.rootUri->file(); else if (Params.rootPath && !Params.rootPath->empty()) @@ -407,6 +410,11 @@ void ClangdLSPServer::onInitialize(const ; if (NegotiatedOffsetEncoding) Result["offsetEncoding"] = *NegotiatedOffsetEncoding; + if (Params.capabilities.SemanticHighlighting) +Result.getObject("capabilities") +->insert( +{"semanticHighlighting", + llvm::json::Object{{"scopes", getTextMateScopeLookupTable()}}}); Reply(std::move(Result)); } @@ -927,6 +935,11 @@ void ClangdLSPServer::applyConfiguration reparseOpenedFiles(); } +void ClangdLSPServer::publishSemanticHighlighting( +SemanticHighlightingParams Params) { + notify("textDocument/semanticHighlighting", Params); +} + void ClangdLSPServer::publishDiagnostics( const URIForFile , std::vector Diagnostics) { // Publish diagnostics. @@ -1063,6 +1076,13 @@ bool ClangdLSPServer::shouldRunCompletio return true; } +void ClangdLSPServer::onHighlightingsReady( +PathRef File, std::vector Highlightings) { + publishSemanticHighlighting( + {{URIForFile::canonicalize(File, /*TUPath=*/File)}, + toSemanticHighlightingInformation(Highlightings)}); +} + void ClangdLSPServer::onDiagnosticsReady(PathRef File, std::vector Diagnostics) { auto URI = URIForFile::canonicalize(File, /*TUPath=*/File); Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.h?rev=365111=365110=365111=diff == --- clang-tools-extra/trunk/clangd/ClangdLSPServer.h (original) +++ clang-tools-extra/trunk/clangd/ClangdLSPServer.h Thu Jul 4 00:53:12 2019 @@ -55,6 +55,8 @@ private: // Implement DiagnosticsConsumer. void onDiagnosticsReady(PathRef File, std::vector Diagnostics) override; void onFileUpdated(PathRef File, const TUStatus ) override; + void onHighlightingsReady(PathRef File, + std::vector Highlightings) override; // LSP methods. Notifications have signature void(const Params&). // Calls have signature void(const Params&, Callback). @@ -115,6 +117,9 @@ private: void reparseOpenedFiles(); void applyConfiguration(const ConfigurationSettings ); + /// Sends a "publishSemanticHighlighting" notification to the LSP client. + void publishSemanticHighlighting(SemanticHighlightingParams Params); + /// Sends a "publishDiagnostics" notification to the LSP client. void publishDiagnostics(const URIForFile , std::vector Diagnostics); Modified: clang-tools-extra/trunk/clangd/Protocol.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Protocol.cpp?rev=365111=365110=365111=diff == --- clang-tools-extra/trunk/clangd/Protocol.cpp (original) +++ clang-tools-extra/trunk/clangd/Protocol.cpp Thu Jul 4 00:53:12 2019 @@ -273,6 +273,12 @@ bool fromJSON(const llvm::json::Value if
[clang-tools-extra] r364747 - [clangd] No longer getting template instantiations from header files in Main AST.
Author: jvikstrom Date: Mon Jul 1 04:49:01 2019 New Revision: 364747 URL: http://llvm.org/viewvc/llvm-project?rev=364747=rev Log: [clangd] No longer getting template instantiations from header files in Main AST. Previous implementation to filter decls not in the main file did not work in the case where a template was instantiated from a header in the main file. It would than include that function/class in topLevelDecls. Differential Revision: https://reviews.llvm.org/D63817 Modified: clang-tools-extra/trunk/clangd/ClangdUnit.cpp clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp Modified: clang-tools-extra/trunk/clangd/ClangdUnit.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdUnit.cpp?rev=364747=364746=364747=diff == --- clang-tools-extra/trunk/clangd/ClangdUnit.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdUnit.cpp Mon Jul 1 04:49:01 2019 @@ -67,7 +67,8 @@ public: bool HandleTopLevelDecl(DeclGroupRef DG) override { for (Decl *D : DG) { - if (D->isFromASTFile()) + auto = D->getASTContext().getSourceManager(); + if (!SM.isWrittenInMainFile(SM.getExpansionLoc(D->getLocation( continue; // ObjCMethodDecl are not actually top-level decls. Modified: clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp?rev=364747=364746=364747=diff == --- clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/ClangdUnitTests.cpp Mon Jul 1 04:49:01 2019 @@ -83,6 +83,26 @@ TEST(ClangdUnitTest, TopLevelDecls) { EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main"))); } +TEST(ClangdUnitTest, DoesNotGetIncludedTopDecls) { + TestTU TU; + TU.HeaderCode = R"cpp( +#define LL void foo(){} +template +struct H { + H() {} + LL +}; + )cpp"; + TU.Code = R"cpp( +int main() { + H h; + h.foo(); +} + )cpp"; + auto AST = TU.build(); + EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(DeclNamed("main"))); +} + TEST(ClangdUnitTest, TokensAfterPreamble) { TestTU TU; TU.AdditionalFiles["foo.h"] = R"( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] r364551 - [clangd] Emit semantic highlighting tokens when the main AST is built.
Author: jvikstrom Date: Thu Jun 27 08:13:03 2019 New Revision: 364551 URL: http://llvm.org/viewvc/llvm-project?rev=364551=rev Log: [clangd] Emit semantic highlighting tokens when the main AST is built. Differential Revision: https://reviews.llvm.org/D63821 Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp clang-tools-extra/trunk/clangd/ClangdServer.h clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=364551=364550=364551=diff == --- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Thu Jun 27 08:13:03 2019 @@ -48,8 +48,10 @@ namespace { // Update the FileIndex with new ASTs and plumb the diagnostics responses. struct UpdateIndexCallbacks : public ParsingCallbacks { - UpdateIndexCallbacks(FileIndex *FIndex, DiagnosticsConsumer ) - : FIndex(FIndex), DiagConsumer(DiagConsumer) {} + UpdateIndexCallbacks(FileIndex *FIndex, DiagnosticsConsumer , + bool SemanticHighlighting) + : FIndex(FIndex), DiagConsumer(DiagConsumer), +SemanticHighlighting(SemanticHighlighting) {} void onPreambleAST(PathRef Path, ASTContext , std::shared_ptr PP, @@ -61,6 +63,8 @@ struct UpdateIndexCallbacks : public Par void onMainAST(PathRef Path, ParsedAST ) override { if (FIndex) FIndex->updateMain(Path, AST); +if (SemanticHighlighting) + DiagConsumer.onHighlightingsReady(Path, getSemanticHighlightings(AST)); } void onDiagnostics(PathRef File, std::vector Diags) override { @@ -74,6 +78,7 @@ struct UpdateIndexCallbacks : public Par private: FileIndex *FIndex; DiagnosticsConsumer + bool SemanticHighlighting; }; } // namespace @@ -82,6 +87,7 @@ ClangdServer::Options ClangdServer::opts Opts.UpdateDebounce = std::chrono::steady_clock::duration::zero(); // Faster! Opts.StorePreamblesInMemory = true; Opts.AsyncThreadsCount = 4; // Consistent! + Opts.SemanticHighlighting = true; return Opts; } @@ -102,10 +108,11 @@ ClangdServer::ClangdServer(const GlobalC // is parsed. // FIXME(ioeric): this can be slow and we may be able to index on less // critical paths. - WorkScheduler(CDB, Opts.AsyncThreadsCount, Opts.StorePreamblesInMemory, -llvm::make_unique(DynamicIdx.get(), -DiagConsumer), -Opts.UpdateDebounce, Opts.RetentionPolicy) { + WorkScheduler( + CDB, Opts.AsyncThreadsCount, Opts.StorePreamblesInMemory, + llvm::make_unique( + DynamicIdx.get(), DiagConsumer, Opts.SemanticHighlighting), + Opts.UpdateDebounce, Opts.RetentionPolicy) { // Adds an index to the stack, at higher priority than existing indexes. auto AddIndex = [&](SymbolIndex *Idx) { if (this->Index != nullptr) { Modified: clang-tools-extra/trunk/clangd/ClangdServer.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.h?rev=364551=364550=364551=diff == --- clang-tools-extra/trunk/clangd/ClangdServer.h (original) +++ clang-tools-extra/trunk/clangd/ClangdServer.h Thu Jun 27 08:13:03 2019 @@ -18,6 +18,7 @@ #include "Function.h" #include "GlobalCompilationDatabase.h" #include "Protocol.h" +#include "SemanticHighlighting.h" #include "TUScheduler.h" #include "XRefs.h" #include "index/Background.h" @@ -49,6 +50,11 @@ public: std::vector Diagnostics) = 0; /// Called whenever the file status is updated. virtual void onFileUpdated(PathRef File, const TUStatus ){}; + + /// Called by ClangdServer when some \p Highlightings for \p File are ready. + virtual void + onHighlightingsReady(PathRef File, + std::vector Highlightings) {} }; /// When set, used by ClangdServer to get clang-tidy options for each particular @@ -131,6 +137,9 @@ public: /// Clangd will execute compiler drivers matching one of these globs to /// fetch system include path. std::vector QueryDriverGlobs; + +/// Enable semantic highlighting features. +bool SemanticHighlighting = false; }; // Sensible default options for use in tests. // Features like indexing must be enabled if desired. @@ -304,7 +313,7 @@ private: // can be caused by missing includes (e.g. member access in incomplete type). bool SuggestMissingIncludes = false; bool EnableHiddenFeatures = false; - + // GUARDED_BY(CachedCompletionFuzzyFindRequestMutex) llvm::StringMap> CachedCompletionFuzzyFindRequestByFile; Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp URL:
[clang-tools-extra] r364421 - [clangd] Added functionality for getting semantic highlights for variable and function declarations
Author: jvikstrom Date: Wed Jun 26 06:08:36 2019 New Revision: 364421 URL: http://llvm.org/viewvc/llvm-project?rev=364421=rev Log: [clangd] Added functionality for getting semantic highlights for variable and function declarations Added: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp clang-tools-extra/trunk/clangd/SemanticHighlighting.h clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Modified: clang-tools-extra/trunk/clangd/CMakeLists.txt clang-tools-extra/trunk/clangd/unittests/CMakeLists.txt Modified: clang-tools-extra/trunk/clangd/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CMakeLists.txt?rev=364421=364420=364421=diff == --- clang-tools-extra/trunk/clangd/CMakeLists.txt (original) +++ clang-tools-extra/trunk/clangd/CMakeLists.txt Wed Jun 26 06:08:36 2019 @@ -63,6 +63,7 @@ add_clang_library(clangDaemon Quality.cpp RIFF.cpp Selection.cpp + SemanticHighlighting.cpp SourceCode.cpp QueryDriverDatabase.cpp Threading.cpp Added: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=364421=auto == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (added) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Wed Jun 26 06:08:36 2019 @@ -0,0 +1,78 @@ +//===--- SemanticHighlighting.cpp - -- ---*- 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 "SemanticHighlighting.h" +#include "Logger.h" +#include "SourceCode.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/RecursiveASTVisitor.h" + +namespace clang { +namespace clangd { +namespace { + +// Collects all semantic tokens in an ASTContext. +class HighlightingTokenCollector +: public RecursiveASTVisitor { + std::vector Tokens; + ASTContext + const SourceManager + +public: + HighlightingTokenCollector(ParsedAST ) + : Ctx(AST.getASTContext()), SM(AST.getSourceManager()) {} + + std::vector collectTokens() { +Tokens.clear(); +TraverseAST(Ctx); +return Tokens; + } + + bool VisitVarDecl(VarDecl *Var) { +addToken(Var, HighlightingKind::Variable); +return true; + } + bool VisitFunctionDecl(FunctionDecl *Func) { +addToken(Func, HighlightingKind::Function); +return true; + } + +private: + void addToken(const NamedDecl *D, HighlightingKind Kind) { +if (D->getLocation().isMacroID()) + // FIXME: skip tokens inside macros for now. + return; + +if (D->getDeclName().isEmpty()) + // Don't add symbols that don't have any length. + return; + +auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation()); +if (!R) { + // R should always have a value, if it doesn't something is very wrong. + elog("Tried to add semantic token with an invalid range"); + return; +} + +Tokens.push_back({Kind, R.getValue()}); + } +}; + +} // namespace + +bool operator==(const HighlightingToken , const HighlightingToken ) { + return Lhs.Kind == Rhs.Kind && Lhs.R == Rhs.R; +} + +std::vector getSemanticHighlightings(ParsedAST ) { + AST.getASTContext().setTraversalScope(AST.getLocalTopLevelDecls()); + return HighlightingTokenCollector(AST).collectTokens(); +} + +} // namespace clangd +} // namespace clang Added: clang-tools-extra/trunk/clangd/SemanticHighlighting.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=364421=auto == --- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (added) +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Wed Jun 26 06:08:36 2019 @@ -0,0 +1,37 @@ +//==-- SemanticHighlighting.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" + +namespace clang { +namespace clangd { + +enum class HighlightingKind { + Variable, + Function, +}; + +// Contains all information needed for the highlighting a token. +struct HighlightingToken { + HighlightingKind Kind; + Range R; +}; + +bool operator==(const HighlightingToken ,