Skip to content

Commit

Permalink
Merged main:275716d6db79 into amd-gfx:707935cc68ad
Browse files Browse the repository at this point in the history
Local branch amd-gfx 707935c Merged main:3dbbadb8ef53 into amd-gfx:67755b8f0509
Remote branch main 275716d [clangd] Derive new signals in CC from ASTSignals.
  • Loading branch information
Sw authored and Sw committed Jan 18, 2021
2 parents 707935c + 275716d commit bcfd672
Show file tree
Hide file tree
Showing 69 changed files with 1,054 additions and 297 deletions.
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ void ClangdServer::codeComplete(PathRef File, Position Pos,
ParseInput.Opts.BuildRecoveryAST = BuildRecoveryAST;
ParseInput.Opts.PreserveRecoveryASTType = PreserveRecoveryASTType;

CodeCompleteOpts.MainFileSignals = IP->Signals;
// FIXME(ibiryukov): even if Preamble is non-null, we may want to check
// both the old and the new version in case only one of them matches.
CodeCompleteResult Result = clangd::codeComplete(
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/CodeComplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,7 @@ class CodeCompleteFlow {
if (PreferredType)
Relevance.HadContextType = true;
Relevance.ContextWords = &ContextWords;
Relevance.MainFileSignals = Opts.MainFileSignals;

auto &First = Bundle.front();
if (auto FuzzyScore = fuzzyScore(First))
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clangd/CodeComplete.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CODECOMPLETE_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CODECOMPLETE_H

#include "ASTSignals.h"
#include "Compiler.h"
#include "Headers.h"
#include "Protocol.h"
Expand Down Expand Up @@ -89,6 +90,7 @@ struct CodeCompleteOptions {
/// clangd.
const SymbolIndex *Index = nullptr;

const ASTSignals *MainFileSignals = nullptr;
/// Include completions that require small corrections, e.g. change '.' to
/// '->' on member access etc.
bool IncludeFixIts = false;
Expand Down
33 changes: 33 additions & 0 deletions clang-tools-extra/clangd/Quality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,38 @@ void SymbolRelevanceSignals::merge(const Symbol &IndexResult) {
if (!(IndexResult.Flags & Symbol::VisibleOutsideFile)) {
Scope = AccessibleScope::FileScope;
}
if (MainFileSignals) {
MainFileRefs =
std::max(MainFileRefs,
MainFileSignals->ReferencedSymbols.lookup(IndexResult.ID));
ScopeRefsInFile =
std::max(ScopeRefsInFile,
MainFileSignals->RelatedNamespaces.lookup(IndexResult.Scope));
}
}

void SymbolRelevanceSignals::computeASTSignals(
const CodeCompletionResult &SemaResult) {
if (!MainFileSignals)
return;
if ((SemaResult.Kind != CodeCompletionResult::RK_Declaration) &&
(SemaResult.Kind != CodeCompletionResult::RK_Pattern))
return;
if (const NamedDecl *ND = SemaResult.getDeclaration()) {
auto ID = getSymbolID(ND);
if (!ID)
return;
MainFileRefs =
std::max(MainFileRefs, MainFileSignals->ReferencedSymbols.lookup(ID));
if (const auto *NSD = dyn_cast<NamespaceDecl>(ND->getDeclContext())) {
if (NSD->isAnonymousNamespace())
return;
std::string Scope = printNamespaceScope(*NSD);
if (!Scope.empty())
ScopeRefsInFile = std::max(
ScopeRefsInFile, MainFileSignals->RelatedNamespaces.lookup(Scope));
}
}
}

void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) {
Expand All @@ -315,6 +347,7 @@ void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) {
InBaseClass |= SemaCCResult.InBaseClass;
}

computeASTSignals(SemaCCResult);
// Declarations are scoped, others (like macros) are assumed global.
if (SemaCCResult.Declaration)
Scope = std::min(Scope, computeScope(SemaCCResult.Declaration));
Expand Down
10 changes: 10 additions & 0 deletions clang-tools-extra/clangd/Quality.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "ExpectedTypes.h"
#include "FileDistance.h"
#include "TUScheduler.h"
#include "clang/Sema/CodeCompleteConsumer.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
Expand Down Expand Up @@ -140,6 +141,14 @@ struct SymbolRelevanceSignals {
/// CompletionPrefix.
unsigned FilterLength = 0;

const ASTSignals *MainFileSignals = nullptr;
/// Number of references to the candidate in the main file.
unsigned MainFileRefs = 0;
/// Number of unique symbols in the main file which belongs to candidate's
/// namespace. This indicates how relevant the namespace is in the current
/// file.
unsigned ScopeRefsInFile = 0;

/// Set of derived signals computed by calculateDerivedSignals(). Must not be
/// set explicitly.
struct DerivedSignals {
Expand All @@ -155,6 +164,7 @@ struct SymbolRelevanceSignals {

void merge(const CodeCompletionResult &SemaResult);
void merge(const Symbol &IndexResult);
void computeASTSignals(const CodeCompletionResult &SemaResult);

// Condense these signals down to a single number, higher is better.
float evaluateHeuristics() const;
Expand Down
46 changes: 46 additions & 0 deletions clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "ASTSignals.h"
#include "Annotations.h"
#include "ClangdServer.h"
#include "CodeComplete.h"
Expand Down Expand Up @@ -51,6 +52,8 @@ using ContextKind = CodeCompletionContext::Kind;

// GMock helpers for matching completion items.
MATCHER_P(Named, Name, "") { return arg.Name == Name; }
MATCHER_P(MainFileRefs, Refs, "") { return arg.MainFileRefs == Refs; }
MATCHER_P(ScopeRefs, Refs, "") { return arg.ScopeRefsInFile == Refs; }
MATCHER_P(NameStartsWith, Prefix, "") {
return llvm::StringRef(arg.Name).startswith(Prefix);
}
Expand Down Expand Up @@ -1110,6 +1113,49 @@ TEST(CompletionTest, RecordCCResultCallback) {
UnorderedElementsAre(Named("xy1"), Named("xy2")));
}

TEST(CompletionTest, ASTSignals) {
struct Completion {
std::string Name;
unsigned MainFileRefs;
unsigned ScopeRefsInFile;
};
CodeCompleteOptions Opts;
std::vector<Completion> RecordedCompletions;
Opts.RecordCCResult = [&RecordedCompletions](const CodeCompletion &CC,
const SymbolQualitySignals &,
const SymbolRelevanceSignals &R,
float Score) {
RecordedCompletions.push_back({CC.Name, R.MainFileRefs, R.ScopeRefsInFile});
};
ASTSignals MainFileSignals;
MainFileSignals.ReferencedSymbols[var("xy1").ID] = 3;
MainFileSignals.ReferencedSymbols[var("xy2").ID] = 1;
MainFileSignals.ReferencedSymbols[var("xyindex").ID] = 10;
MainFileSignals.RelatedNamespaces["tar::"] = 5;
MainFileSignals.RelatedNamespaces["bar::"] = 3;
Opts.MainFileSignals = &MainFileSignals;
Opts.AllScopes = true;
completions(
R"cpp(
int xy1;
int xy2;
namespace bar {
int xybar = 1;
int a = xy^
}
)cpp",
/*IndexSymbols=*/{var("xyindex"), var("tar::xytar"), var("bar::xybar")},
Opts);
EXPECT_THAT(RecordedCompletions,
UnorderedElementsAre(
AllOf(Named("xy1"), MainFileRefs(3u), ScopeRefs(0u)),
AllOf(Named("xy2"), MainFileRefs(1u), ScopeRefs(0u)),
AllOf(Named("xyindex"), MainFileRefs(10u), ScopeRefs(0u)),
AllOf(Named("xytar"), MainFileRefs(0u), ScopeRefs(5u)),
AllOf(/*both from sema and index*/ Named("xybar"),
MainFileRefs(0u), ScopeRefs(3u))));
}

SignatureHelp signatures(llvm::StringRef Text, Position Point,
std::vector<Symbol> IndexSymbols = {}) {
std::unique_ptr<SymbolIndex> Index;
Expand Down
8 changes: 0 additions & 8 deletions lld/test/wasm/alias.s
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ _start:
# CHECK-NEXT: ReturnTypes: []
# CHECK-NEXT: - Type: FUNCTION
# CHECK-NEXT: FunctionTypes: [ 0 ]
# CHECK-NEXT: - Type: TABLE
# CHECK-NEXT: Tables:
# CHECK-NEXT: - Index: 0
# CHECK-NEXT: ElemType: FUNCREF
# CHECK-NEXT: Limits:
# CHECK-NEXT: Flags: [ HAS_MAX ]
# CHECK-NEXT: Initial: 0x1
# CHECK-NEXT: Maximum: 0x1
# CHECK-NEXT: - Type: MEMORY
# CHECK-NEXT: Memories:
# CHECK-NEXT: - Initial: 0x2
Expand Down
31 changes: 31 additions & 0 deletions lld/test/wasm/export-table-explicit.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/start.s -o %t.start.o
# RUN: wasm-ld --export-table -o %t.wasm %t.start.o
# RUN: obj2yaml %t.wasm | FileCheck %s

# Verify the interaction between --export-table and declared tables

.globl __indirect_function_table
.tabletype __indirect_function_table,externref

# CHECK: - Type: TABLE
# CHECK-NEXT: Tables:
# CHECK-NEXT: - Index: 0
# CHECK-NEXT: ElemType: FUNCREF
# CHECK-NEXT: Limits:
# CHECK-NEXT: Flags: [ HAS_MAX ]
# CHECK-NEXT: Initial: 0x1
# CHECK-NEXT: Maximum: 0x1
# CHECK-NEXT: - Type:

# CHECK: - Type: EXPORT
# CHECK-NEXT: Exports:
# CHECK-NEXT: - Name: memory
# CHECK-NEXT: Kind: MEMORY
# CHECK-NEXT: Index: 0
# CHECK-NEXT: - Name: _start
# CHECK-NEXT: Kind: FUNCTION
# CHECK-NEXT: Index: 0
# CHECK-NEXT: - Name: __indirect_function_table
# CHECK-NEXT: Kind: TABLE
# CHECK-NEXT: Index: 0
# CHECK-NEXT: - Type:
18 changes: 9 additions & 9 deletions lld/test/wasm/init-fini.ll
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ entry:
; RELOC-NEXT: InitFunctions [
; RELOC-NEXT: 0 (priority=101)
; RELOC-NEXT: 1 (priority=101)
; RELOC-NEXT: 14 (priority=101)
; RELOC-NEXT: 10 (priority=101)
; RELOC-NEXT: 20 (priority=101)
; RELOC-NEXT: 10 (priority=202)
; RELOC-NEXT: 22 (priority=202)
; RELOC-NEXT: 15 (priority=101)
; RELOC-NEXT: 11 (priority=101)
; RELOC-NEXT: 21 (priority=101)
; RELOC-NEXT: 11 (priority=202)
; RELOC-NEXT: 23 (priority=202)
; RELOC-NEXT: 0 (priority=1001)
; RELOC-NEXT: 16 (priority=1001)
; RELOC-NEXT: 10 (priority=2002)
; RELOC-NEXT: 24 (priority=2002)
; RELOC-NEXT: 17 (priority=1001)
; RELOC-NEXT: 11 (priority=2002)
; RELOC-NEXT: 25 (priority=2002)
; RELOC-NEXT: 9 (priority=4000)
; RELOC-NEXT: 18 (priority=4000)
; RELOC-NEXT: 19 (priority=4000)
; RELOC-NEXT: ]
8 changes: 0 additions & 8 deletions lld/test/wasm/local-symbols.ll
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ entry:
; CHECK-NEXT: ReturnTypes: []
; CHECK-NEXT: - Type: FUNCTION
; CHECK-NEXT: FunctionTypes: [ 0, 1 ]
; CHECK-NEXT: - Type: TABLE
; CHECK-NEXT: Tables:
; CHECK-NEXT: - Index: 0
; CHECK-NEXT: ElemType: FUNCREF
; CHECK-NEXT: Limits:
; CHECK-NEXT: Flags: [ HAS_MAX ]
; CHECK-NEXT: Initial: 0x1
; CHECK-NEXT: Maximum: 0x1
; CHECK-NEXT: - Type: MEMORY
; CHECK-NEXT: Memories:
; CHECK-NEXT: - Initial: 0x2
Expand Down
Loading

0 comments on commit bcfd672

Please sign in to comment.