Skip to content

Commit 48484b4

Browse files
authored
Merge pull request #1002
LLVM pull down (356b335)
2 parents abc6a59 + 0461c77 commit 48484b4

File tree

396 files changed

+8319
-4315
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

396 files changed

+8319
-4315
lines changed

clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@ MagicNumbersCheck::MagicNumbersCheck(StringRef Name, ClangTidyContext *Context)
8686
IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
8787
for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
8888
llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
89-
if (!FloatValue.convertFromString(InputValue, DefaultRoundingMode)) {
90-
assert(false && "Invalid floating point representation");
91-
}
89+
auto StatusOrErr =
90+
FloatValue.convertFromString(InputValue, DefaultRoundingMode);
91+
assert(StatusOrErr && "Invalid floating point representation");
92+
consumeError(StatusOrErr.takeError());
9293
IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
9394

9495
llvm::APFloat DoubleValue(llvm::APFloat::IEEEdouble());
95-
if (!DoubleValue.convertFromString(InputValue, DefaultRoundingMode)) {
96-
assert(false && "Invalid floating point representation");
97-
}
96+
StatusOrErr =
97+
DoubleValue.convertFromString(InputValue, DefaultRoundingMode);
98+
assert(StatusOrErr && "Invalid floating point representation");
99+
consumeError(StatusOrErr.takeError());
98100
IgnoredDoublePointValues.push_back(DoubleValue.convertToDouble());
99101
}
100102
llvm::sort(IgnoredFloatingPointValues.begin(),

clang-tools-extra/clangd/CompileCommands.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ void CommandMangler::adjust(std::vector<std::string> &Cmd) const {
155155
if (ResourceDir && !Has("-resource-dir"))
156156
Cmd.push_back(("-resource-dir=" + *ResourceDir));
157157

158-
if (Sysroot && !Has("-isysroot")) {
158+
// Don't set `-isysroot` if it is already set or if `--sysroot` is set.
159+
// `--sysroot` is a superset of the `-isysroot` argument.
160+
if (Sysroot && !Has("-isysroot") && !Has("--sysroot")) {
159161
Cmd.push_back("-isysroot");
160162
Cmd.push_back(*Sysroot);
161163
}

clang-tools-extra/clangd/Diagnostics.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "clang/Lex/Token.h"
2323
#include "llvm/ADT/ArrayRef.h"
2424
#include "llvm/ADT/DenseSet.h"
25+
#include "llvm/ADT/Optional.h"
26+
#include "llvm/ADT/STLExtras.h"
2527
#include "llvm/ADT/SmallString.h"
2628
#include "llvm/ADT/StringRef.h"
2729
#include "llvm/ADT/Twine.h"
@@ -328,14 +330,22 @@ CodeAction toCodeAction(const Fix &F, const URIForFile &File) {
328330
void toLSPDiags(
329331
const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts,
330332
llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn) {
331-
auto FillBasicFields = [](const DiagBase &D) -> clangd::Diagnostic {
332-
clangd::Diagnostic Res;
333-
Res.range = D.Range;
334-
Res.severity = getSeverity(D.Severity);
335-
return Res;
336-
};
333+
clangd::Diagnostic Main;
334+
Main.severity = getSeverity(D.Severity);
335+
336+
// Main diagnostic should always refer to a range inside main file. If a
337+
// diagnostic made it so for, it means either itself or one of its notes is
338+
// inside main file.
339+
if (D.InsideMainFile) {
340+
Main.range = D.Range;
341+
} else {
342+
auto It =
343+
llvm::find_if(D.Notes, [](const Note &N) { return N.InsideMainFile; });
344+
assert(It != D.Notes.end() &&
345+
"neither the main diagnostic nor notes are inside main file");
346+
Main.range = It->Range;
347+
}
337348

338-
clangd::Diagnostic Main = FillBasicFields(D);
339349
Main.code = D.Name;
340350
switch (D.Source) {
341351
case Diag::Clang:
@@ -379,7 +389,9 @@ void toLSPDiags(
379389
for (auto &Note : D.Notes) {
380390
if (!Note.InsideMainFile)
381391
continue;
382-
clangd::Diagnostic Res = FillBasicFields(Note);
392+
clangd::Diagnostic Res;
393+
Res.severity = getSeverity(Note.Severity);
394+
Res.range = Note.Range;
383395
Res.message = noteMessage(D, Note, Opts);
384396
OutFn(std::move(Res), llvm::ArrayRef<Fix>());
385397
}

clang-tools-extra/clangd/Hover.cpp

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
#include "clang/AST/PrettyPrinter.h"
2626
#include "clang/Index/IndexSymbol.h"
2727
#include "llvm/ADT/STLExtras.h"
28-
#include "llvm/ADT/iterator_range.h"
29-
#include "llvm/Support/Casting.h"
28+
#include "llvm/ADT/SmallVector.h"
29+
#include "llvm/ADT/StringExtras.h"
30+
#include "llvm/ADT/StringRef.h"
3031
#include "llvm/Support/raw_ostream.h"
32+
#include <string>
3133

3234
namespace clang {
3335
namespace clangd {
@@ -224,8 +226,8 @@ void enhanceFromIndex(HoverInfo &Hover, const NamedDecl &ND,
224226

225227
// Populates Type, ReturnType, and Parameters for function-like decls.
226228
void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D,
227-
const FunctionDecl *FD,
228-
const PrintingPolicy &Policy) {
229+
const FunctionDecl *FD,
230+
const PrintingPolicy &Policy) {
229231
HI.Parameters.emplace();
230232
for (const ParmVarDecl *PVD : FD->parameters()) {
231233
HI.Parameters->emplace_back();
@@ -250,11 +252,11 @@ void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D,
250252
}
251253
}
252254

253-
if (const auto* CCD = llvm::dyn_cast<CXXConstructorDecl>(FD)) {
255+
if (const auto *CCD = llvm::dyn_cast<CXXConstructorDecl>(FD)) {
254256
// Constructor's "return type" is the class type.
255257
HI.ReturnType = declaredType(CCD->getParent()).getAsString(Policy);
256258
// Don't provide any type for the constructor itself.
257-
} else if (llvm::isa<CXXDestructorDecl>(FD)){
259+
} else if (llvm::isa<CXXDestructorDecl>(FD)) {
258260
HI.ReturnType = "void";
259261
} else {
260262
HI.ReturnType = FD->getReturnType().getAsString(Policy);
@@ -309,7 +311,7 @@ llvm::Optional<std::string> printExprValue(const SelectionTree::Node *N,
309311
}
310312

311313
/// Generate a \p Hover object given the declaration \p D.
312-
HoverInfo getHoverContents(const Decl *D, const SymbolIndex *Index) {
314+
HoverInfo getHoverContents(const NamedDecl *D, const SymbolIndex *Index) {
313315
HoverInfo HI;
314316
const ASTContext &Ctx = D->getASTContext();
315317

@@ -321,12 +323,10 @@ HoverInfo getHoverContents(const Decl *D, const SymbolIndex *Index) {
321323
HI.LocalScope.append("::");
322324

323325
PrintingPolicy Policy = printingPolicyForDecls(Ctx.getPrintingPolicy());
324-
if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D)) {
325-
HI.Name = printName(Ctx, *ND);
326-
ND = getDeclForComment(ND);
327-
HI.Documentation = getDeclComment(Ctx, *ND);
328-
enhanceFromIndex(HI, *ND, Index);
329-
}
326+
HI.Name = printName(Ctx, *D);
327+
const auto *CommentD = getDeclForComment(D);
328+
HI.Documentation = getDeclComment(Ctx, *CommentD);
329+
enhanceFromIndex(HI, *CommentD, Index);
330330

331331
HI.Kind = index::getSymbolInfo(D).Kind;
332332

@@ -460,34 +460,70 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
460460
tooling::applyAllReplacements(HI->Definition, Replacements))
461461
HI->Definition = *Formatted;
462462

463-
HI->SymRange = getTokenRange(AST.getSourceManager(),
464-
AST.getLangOpts(), SourceLocationBeg);
463+
HI->SymRange = getTokenRange(AST.getSourceManager(), AST.getLangOpts(),
464+
SourceLocationBeg);
465465
return HI;
466466
}
467467

468468
markup::Document HoverInfo::present() const {
469469
markup::Document Output;
470-
if (NamespaceScope) {
471-
auto &P = Output.addParagraph();
472-
P.appendText("Declared in");
473-
// Drop trailing "::".
474-
if (!LocalScope.empty())
475-
P.appendCode(llvm::StringRef(LocalScope).drop_back(2));
476-
else if (NamespaceScope->empty())
477-
P.appendCode("global namespace");
478-
else
479-
P.appendCode(llvm::StringRef(*NamespaceScope).drop_back(2));
470+
// Header contains a text of the form:
471+
// variable `var` : `int`
472+
//
473+
// class `X`
474+
//
475+
// function `foo` → `int`
476+
markup::Paragraph &Header = Output.addParagraph();
477+
Header.appendText(index::getSymbolKindString(Kind));
478+
assert(!Name.empty() && "hover triggered on a nameless symbol");
479+
Header.appendCode(Name);
480+
if (ReturnType) {
481+
Header.appendText("");
482+
Header.appendCode(*ReturnType);
483+
} else if (Type) {
484+
Header.appendText(":");
485+
Header.appendCode(*Type);
480486
}
481487

482-
if (!Definition.empty()) {
483-
Output.addCodeBlock(Definition);
484-
} else {
485-
// Builtin types
486-
Output.addCodeBlock(Name);
488+
// For functions we display signature in a list form, e.g.:
489+
// - `bool param1`
490+
// - `int param2 = 5`
491+
if (Parameters && !Parameters->empty()) {
492+
markup::BulletList &L = Output.addBulletList();
493+
for (const auto &Param : *Parameters) {
494+
std::string Buffer;
495+
llvm::raw_string_ostream OS(Buffer);
496+
OS << Param;
497+
L.addItem().addParagraph().appendCode(std::move(OS.str()));
498+
}
499+
}
500+
501+
if (Value) {
502+
markup::Paragraph &P = Output.addParagraph();
503+
P.appendText("Value =");
504+
P.appendCode(*Value);
487505
}
488506

489507
if (!Documentation.empty())
490508
Output.addParagraph().appendText(Documentation);
509+
510+
if (!Definition.empty()) {
511+
std::string ScopeComment;
512+
// Drop trailing "::".
513+
if (!LocalScope.empty()) {
514+
// Container name, e.g. class, method, function.
515+
// We might want to propogate some info about container type to print
516+
// function foo, class X, method X::bar, etc.
517+
ScopeComment =
518+
"// In " + llvm::StringRef(LocalScope).rtrim(':').str() + '\n';
519+
} else if (NamespaceScope && !NamespaceScope->empty()) {
520+
ScopeComment = "// In namespace " +
521+
llvm::StringRef(*NamespaceScope).rtrim(':').str() + '\n';
522+
}
523+
// Note that we don't print anything for global namespace, to not annoy
524+
// non-c++ projects or projects that are not making use of namespaces.
525+
Output.addCodeBlock(ScopeComment + Definition);
526+
}
491527
return Output;
492528
}
493529

clang-tools-extra/clangd/test/hover.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# CHECK-NEXT: "result": {
1010
# CHECK-NEXT: "contents": {
1111
# CHECK-NEXT: "kind": "plaintext",
12-
# CHECK-NEXT: "value": "Declared in global namespace\n\nvoid foo()"
12+
# CHECK-NEXT: "value": "function foo → void\n\nvoid foo()"
1313
# CHECK-NEXT: },
1414
# CHECK-NEXT: "range": {
1515
# CHECK-NEXT: "end": {
@@ -37,7 +37,7 @@
3737
# CHECK-NEXT: "result": {
3838
# CHECK-NEXT: "contents": {
3939
# CHECK-NEXT: "kind": "plaintext",
40-
# CHECK-NEXT: "value": "Declared in global namespace\n\nenum foo {}"
40+
# CHECK-NEXT: "value": "enum foo\n\nenum foo {}"
4141
# CHECK-NEXT: },
4242
# CHECK-NEXT: "range": {
4343
# CHECK-NEXT: "end": {

clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,29 @@ TEST(IgnoreDiags, FromNonWrittenInclude) {
10141014
EXPECT_THAT(TU.build().getDiagnostics(), UnorderedElementsAre());
10151015
}
10161016

1017+
TEST(ToLSPDiag, RangeIsInMain) {
1018+
ClangdDiagnosticOptions Opts;
1019+
clangd::Diag D;
1020+
D.Range = {pos(1, 2), pos(3, 4)};
1021+
D.Notes.emplace_back();
1022+
Note &N = D.Notes.back();
1023+
N.Range = {pos(2, 3), pos(3, 4)};
1024+
1025+
D.InsideMainFile = true;
1026+
N.InsideMainFile = false;
1027+
toLSPDiags(D, {}, Opts,
1028+
[&](clangd::Diagnostic LSPDiag, ArrayRef<clangd::Fix>) {
1029+
EXPECT_EQ(LSPDiag.range, D.Range);
1030+
});
1031+
1032+
D.InsideMainFile = false;
1033+
N.InsideMainFile = true;
1034+
toLSPDiags(D, {}, Opts,
1035+
[&](clangd::Diagnostic LSPDiag, ArrayRef<clangd::Fix>) {
1036+
EXPECT_EQ(LSPDiag.range, N.Range);
1037+
});
1038+
}
1039+
10171040
} // namespace
10181041
} // namespace clangd
10191042
} // namespace clang

clang-tools-extra/clangd/unittests/HoverTests.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,95 @@ TEST(Hover, DocsFromMostSpecial) {
16061606
}
16071607
}
16081608
}
1609+
TEST(Hover, Present) {
1610+
struct {
1611+
const std::function<void(HoverInfo &)> Builder;
1612+
llvm::StringRef ExpectedRender;
1613+
} Cases[] = {
1614+
{
1615+
[](HoverInfo &HI) {
1616+
HI.Kind = index::SymbolKind::Unknown;
1617+
HI.Name = "X";
1618+
},
1619+
R"(<unknown> X)",
1620+
},
1621+
{
1622+
[](HoverInfo &HI) {
1623+
HI.Kind = index::SymbolKind::NamespaceAlias;
1624+
HI.Name = "foo";
1625+
},
1626+
R"(namespace-alias foo)",
1627+
},
1628+
{
1629+
[](HoverInfo &HI) {
1630+
HI.Kind = index::SymbolKind::Class;
1631+
HI.TemplateParameters = {
1632+
{std::string("typename"), std::string("T"), llvm::None},
1633+
{std::string("typename"), std::string("C"),
1634+
std::string("bool")},
1635+
};
1636+
HI.Documentation = "documentation";
1637+
HI.Definition =
1638+
"template <typename T, typename C = bool> class Foo {}";
1639+
HI.Name = "foo";
1640+
HI.NamespaceScope.emplace();
1641+
},
1642+
R"(class foo
1643+
documentation
1644+
1645+
template <typename T, typename C = bool> class Foo {})",
1646+
},
1647+
{
1648+
[](HoverInfo &HI) {
1649+
HI.Kind = index::SymbolKind::Function;
1650+
HI.Name = "foo";
1651+
HI.Type = "type";
1652+
HI.ReturnType = "ret_type";
1653+
HI.Parameters.emplace();
1654+
HoverInfo::Param P;
1655+
HI.Parameters->push_back(P);
1656+
P.Type = "type";
1657+
HI.Parameters->push_back(P);
1658+
P.Name = "foo";
1659+
HI.Parameters->push_back(P);
1660+
P.Default = "default";
1661+
HI.Parameters->push_back(P);
1662+
HI.NamespaceScope = "ns::";
1663+
HI.Definition = "ret_type foo(params) {}";
1664+
},
1665+
R"(function foo → ret_type
1666+
-
1667+
- type
1668+
- type foo
1669+
- type foo = default
1670+
1671+
// In namespace ns
1672+
ret_type foo(params) {})",
1673+
},
1674+
{
1675+
[](HoverInfo &HI) {
1676+
HI.Kind = index::SymbolKind::Variable;
1677+
HI.LocalScope = "test::bar::";
1678+
HI.Value = "value";
1679+
HI.Name = "foo";
1680+
HI.Type = "type";
1681+
HI.Definition = "def";
1682+
},
1683+
R"(variable foo : type
1684+
Value = value
1685+
1686+
// In test::bar
1687+
def)",
1688+
},
1689+
};
1690+
1691+
for (const auto &C : Cases) {
1692+
HoverInfo HI;
1693+
C.Builder(HI);
1694+
EXPECT_EQ(HI.present().asPlainText(), C.ExpectedRender);
1695+
}
1696+
}
1697+
16091698
} // namespace
16101699
} // namespace clangd
16111700
} // namespace clang

0 commit comments

Comments
 (0)