Skip to content

Commit b0e9d6d

Browse files
committed
Merge from 'main' to 'sycl-web' (#199)
CONFLICT (content): Merge conflict in clang/lib/Sema/SemaDecl.cpp
2 parents f8dda28 + c163aae commit b0e9d6d

File tree

887 files changed

+30716
-9086
lines changed

Some content is hidden

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

887 files changed

+30716
-9086
lines changed

clang-tools-extra/clangd/AST.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,27 @@ SymbolID getSymbolID(const llvm::StringRef MacroName, const MacroInfo *MI,
299299
return SymbolID(USR);
300300
}
301301

302-
// FIXME: This should be handled while printing underlying decls instead.
303302
std::string printType(const QualType QT, const DeclContext &CurContext) {
304303
std::string Result;
305304
llvm::raw_string_ostream OS(Result);
306-
auto Decls =
307-
explicitReferenceTargets(DynTypedNode::create(QT), DeclRelation::Alias);
308-
if (!Decls.empty())
309-
OS << getQualification(CurContext.getParentASTContext(), &CurContext,
310-
Decls.front(),
311-
/*VisibleNamespaces=*/llvm::ArrayRef<std::string>{});
312305
PrintingPolicy PP(CurContext.getParentASTContext().getPrintingPolicy());
313-
PP.SuppressScope = true;
314306
PP.SuppressTagKeyword = true;
307+
PP.SuppressUnwrittenScope = true;
308+
309+
class PrintCB : public PrintingCallbacks {
310+
public:
311+
PrintCB(const DeclContext *CurContext) : CurContext(CurContext) {}
312+
virtual ~PrintCB() {}
313+
virtual bool isScopeVisible(const DeclContext *DC) const override {
314+
return DC->Encloses(CurContext);
315+
}
316+
317+
private:
318+
const DeclContext *CurContext;
319+
};
320+
PrintCB PCB(&CurContext);
321+
PP.Callbacks = &PCB;
322+
315323
QT.print(OS, PP);
316324
return OS.str();
317325
}

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,9 @@ struct CompletionRecorder : public CodeCompleteConsumer {
828828
};
829829

830830
struct ScoredSignature {
831-
// When set, requires documentation to be requested from the index with this
832-
// ID.
833-
llvm::Optional<SymbolID> IDForDoc;
831+
// When not null, requires documentation to be requested from the index with
832+
// this ID.
833+
SymbolID IDForDoc;
834834
SignatureInformation Signature;
835835
SignatureQualitySignals Quality;
836836
};
@@ -894,7 +894,7 @@ class SignatureHelpCollector final : public CodeCompleteConsumer {
894894
for (const auto &S : ScoredSignatures) {
895895
if (!S.IDForDoc)
896896
continue;
897-
IndexRequest.IDs.insert(*S.IDForDoc);
897+
IndexRequest.IDs.insert(S.IDForDoc);
898898
}
899899
Index->lookup(IndexRequest, [&](const Symbol &S) {
900900
if (!S.Documentation.empty())
@@ -939,7 +939,7 @@ class SignatureHelpCollector final : public CodeCompleteConsumer {
939939

940940
for (auto &SS : ScoredSignatures) {
941941
auto IndexDocIt =
942-
SS.IDForDoc ? FetchedDocs.find(*SS.IDForDoc) : FetchedDocs.end();
942+
SS.IDForDoc ? FetchedDocs.find(SS.IDForDoc) : FetchedDocs.end();
943943
if (IndexDocIt != FetchedDocs.end())
944944
SS.Signature.documentation = IndexDocIt->second;
945945

@@ -1111,8 +1111,8 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
11111111
offsetToClangLineColumn(Input.ParseInput.Contents, Input.Offset);
11121112

11131113
std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer =
1114-
llvm::MemoryBuffer::getMemBufferCopy(Input.ParseInput.Contents,
1115-
Input.FileName);
1114+
llvm::MemoryBuffer::getMemBuffer(Input.ParseInput.Contents,
1115+
Input.FileName);
11161116
// The diagnostic options must be set before creating a CompilerInstance.
11171117
CI->getDiagnosticOpts().IgnoreWarnings = true;
11181118
// We reuse the preamble whether it's valid or not. This is a

clang-tools-extra/clangd/XRefs.cpp

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,25 @@ const NamedDecl *getPreferredDecl(const NamedDecl *D) {
292292
return D;
293293
}
294294

295-
std::vector<LocatedSymbol> findOverrides(llvm::DenseSet<SymbolID> IDs,
296-
const SymbolIndex *Index,
297-
llvm::StringRef MainFilePath) {
295+
std::vector<LocatedSymbol> findImplementors(llvm::DenseSet<SymbolID> IDs,
296+
RelationKind Predicate,
297+
const SymbolIndex *Index,
298+
llvm::StringRef MainFilePath) {
298299
if (IDs.empty())
299300
return {};
301+
static constexpr trace::Metric FindImplementorsMetric(
302+
"find_implementors", trace::Metric::Counter, "case");
303+
switch (Predicate) {
304+
case RelationKind::BaseOf:
305+
FindImplementorsMetric.record(1, "find-base");
306+
break;
307+
case RelationKind::OverriddenBy:
308+
FindImplementorsMetric.record(1, "find-override");
309+
break;
310+
}
311+
300312
RelationsRequest Req;
301-
Req.Predicate = RelationKind::OverriddenBy;
313+
Req.Predicate = Predicate;
302314
Req.Subjects = std::move(IDs);
303315
std::vector<LocatedSymbol> Results;
304316
Index->relations(Req, [&](const SymbolID &Subject, const Symbol &Object) {
@@ -335,6 +347,8 @@ locateASTReferent(SourceLocation CurLoc, const syntax::Token *TouchedIdentifier,
335347
// Keep track of SymbolID -> index mapping, to fill in index data later.
336348
llvm::DenseMap<SymbolID, size_t> ResultIndex;
337349

350+
static constexpr trace::Metric LocateASTReferentMetric(
351+
"locate_ast_referent", trace::Metric::Counter, "case");
338352
auto AddResultDecl = [&](const NamedDecl *D) {
339353
D = getPreferredDecl(D);
340354
auto Loc =
@@ -368,8 +382,10 @@ locateASTReferent(SourceLocation CurLoc, const syntax::Token *TouchedIdentifier,
368382
// saved in the AST.
369383
if (CMD->isPure()) {
370384
if (TouchedIdentifier && SM.getSpellingLoc(CMD->getLocation()) ==
371-
TouchedIdentifier->location())
385+
TouchedIdentifier->location()) {
372386
VirtualMethods.insert(getSymbolID(CMD));
387+
LocateASTReferentMetric.record(1, "method-to-override");
388+
}
373389
}
374390
// Special case: void foo() ^override: jump to the overridden method.
375391
const InheritableAttr *Attr = D->getAttr<OverrideAttr>();
@@ -378,6 +394,7 @@ locateASTReferent(SourceLocation CurLoc, const syntax::Token *TouchedIdentifier,
378394
if (Attr && TouchedIdentifier &&
379395
SM.getSpellingLoc(Attr->getLocation()) ==
380396
TouchedIdentifier->location()) {
397+
LocateASTReferentMetric.record(1, "method-to-base");
381398
// We may be overridding multiple methods - offer them all.
382399
for (const NamedDecl *ND : CMD->overridden_methods())
383400
AddResultDecl(ND);
@@ -402,6 +419,7 @@ locateASTReferent(SourceLocation CurLoc, const syntax::Token *TouchedIdentifier,
402419
if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
403420
if (TouchedIdentifier &&
404421
D->getLocation() == TouchedIdentifier->location()) {
422+
LocateASTReferentMetric.record(1, "template-specialization-to-primary");
405423
AddResultDecl(CTSD->getSpecializedTemplate());
406424
continue;
407425
}
@@ -417,9 +435,12 @@ locateASTReferent(SourceLocation CurLoc, const syntax::Token *TouchedIdentifier,
417435
if (const auto *ID = CD->getClassInterface())
418436
if (TouchedIdentifier &&
419437
(CD->getLocation() == TouchedIdentifier->location() ||
420-
ID->getName() == TouchedIdentifier->text(SM)))
438+
ID->getName() == TouchedIdentifier->text(SM))) {
439+
LocateASTReferentMetric.record(1, "objc-category-to-class");
421440
AddResultDecl(ID);
441+
}
422442

443+
LocateASTReferentMetric.record(1, "regular");
423444
// Otherwise the target declaration is the right one.
424445
AddResultDecl(D);
425446
}
@@ -458,7 +479,8 @@ locateASTReferent(SourceLocation CurLoc, const syntax::Token *TouchedIdentifier,
458479
});
459480
}
460481

461-
auto Overrides = findOverrides(VirtualMethods, Index, MainFilePath);
482+
auto Overrides = findImplementors(VirtualMethods, RelationKind::OverriddenBy,
483+
Index, MainFilePath);
462484
Result.insert(Result.end(), Overrides.begin(), Overrides.end());
463485
return Result;
464486
}
@@ -1239,12 +1261,20 @@ std::vector<LocatedSymbol> findImplementations(ParsedAST &AST, Position Pos,
12391261
}
12401262
DeclRelationSet Relations =
12411263
DeclRelation::TemplatePattern | DeclRelation::Alias;
1242-
llvm::DenseSet<SymbolID> VirtualMethods;
1243-
for (const NamedDecl *ND : getDeclAtPosition(AST, *CurLoc, Relations))
1244-
if (const CXXMethodDecl *CXXMD = llvm::dyn_cast<CXXMethodDecl>(ND))
1245-
if (CXXMD->isVirtual())
1246-
VirtualMethods.insert(getSymbolID(ND));
1247-
return findOverrides(std::move(VirtualMethods), Index, *MainFilePath);
1264+
llvm::DenseSet<SymbolID> IDs;
1265+
RelationKind QueryKind;
1266+
for (const NamedDecl *ND : getDeclAtPosition(AST, *CurLoc, Relations)) {
1267+
if (const auto *CXXMD = llvm::dyn_cast<CXXMethodDecl>(ND)) {
1268+
if (CXXMD->isVirtual()) {
1269+
IDs.insert(getSymbolID(ND));
1270+
QueryKind = RelationKind::OverriddenBy;
1271+
}
1272+
} else if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) {
1273+
IDs.insert(getSymbolID(RD));
1274+
QueryKind = RelationKind::BaseOf;
1275+
}
1276+
}
1277+
return findImplementors(std::move(IDs), QueryKind, Index, *MainFilePath);
12481278
}
12491279

12501280
ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,

clang-tools-extra/clangd/XRefs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ struct ReferencesResult {
8383
bool HasMore = false;
8484
};
8585

86-
/// Returns implementations of the virtual function at a specified \p Pos.
86+
/// Returns implementations at a specified \p Pos:
87+
/// - overrides for a virtual method;
88+
/// - subclasses for a base class;
8789
std::vector<LocatedSymbol> findImplementations(ParsedAST &AST, Position Pos,
8890
const SymbolIndex *Index);
8991

clang-tools-extra/clangd/index/Merge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void MergedIndex::relations(
164164

165165
// Returns true if \p L is (strictly) preferred to \p R (e.g. by file paths). If
166166
// neither is preferred, this returns false.
167-
bool prefer(const SymbolLocation &L, const SymbolLocation &R) {
167+
static bool prefer(const SymbolLocation &L, const SymbolLocation &R) {
168168
if (!L)
169169
return false;
170170
if (!R)

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,11 +1611,11 @@ TEST(LocateSymbol, NearbyIdentifier) {
16111611

16121612
TEST(FindImplementations, Inheritance) {
16131613
llvm::StringRef Test = R"cpp(
1614-
struct Base {
1614+
struct $0^Base {
16151615
virtual void F$1^oo();
16161616
void C$4^oncrete();
16171617
};
1618-
struct Child1 : Base {
1618+
struct $0[[Child1]] : Base {
16191619
void $1[[Fo$3^o]]() override;
16201620
virtual void B$2^ar();
16211621
void Concrete(); // No implementations for concrete methods.
@@ -1625,20 +1625,24 @@ TEST(FindImplementations, Inheritance) {
16251625
void $2[[Bar]]() override;
16261626
};
16271627
void FromReference() {
1628-
Base* B;
1628+
$0^Base* B;
16291629
B->Fo$1^o();
16301630
B->C$4^oncrete();
16311631
&Base::Fo$1^o;
16321632
Child1 * C1;
16331633
C1->B$2^ar();
16341634
C1->Fo$3^o();
16351635
}
1636+
// CRTP should work.
1637+
template<typename T>
1638+
struct $5^TemplateBase {};
1639+
struct $5[[Child3]] : public TemplateBase<Child3> {};
16361640
)cpp";
16371641

16381642
Annotations Code(Test);
16391643
auto TU = TestTU::withCode(Code.code());
16401644
auto AST = TU.build();
1641-
for (const std::string &Label : {"1", "2", "3", "4"}) {
1645+
for (StringRef Label : {"0", "1", "2", "3", "4", "5"}) {
16421646
for (const auto &Point : Code.points(Label)) {
16431647
EXPECT_THAT(findImplementations(AST, Point, TU.index().get()),
16441648
UnorderedPointwise(DeclRange(), Code.ranges(Label)))

clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ TEST_F(ExpandAutoTypeTest, Test) {
6565
EXPECT_EQ(apply(R"cpp(au^to x = "test";)cpp"),
6666
R"cpp(const char * x = "test";)cpp");
6767

68+
EXPECT_EQ(apply("ns::Class * foo() { au^to c = foo(); }"),
69+
"ns::Class * foo() { ns::Class * c = foo(); }");
70+
EXPECT_EQ(apply("void ns::Func() { au^to x = new ns::Class::Nested{}; }"),
71+
"void ns::Func() { Class::Nested * x = new ns::Class::Nested{}; }");
72+
6873
EXPECT_UNAVAILABLE("dec^ltype(au^to) x = 10;");
6974
// expanding types in structured bindings is syntactically invalid.
7075
EXPECT_UNAVAILABLE("const ^auto &[x,y] = (int[]){1,2};");

clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ void f(const int c) {
9797
})cpp";
9898
EXPECT_EQ(apply(ConstCheckInput), ConstCheckOutput);
9999

100+
// Check const qualifier with namespace
101+
std::string ConstNamespaceCheckInput = R"cpp(
102+
namespace X { struct Y { int z; }; }
103+
int f(const X::Y &y) {
104+
[[return y.z + y.z;]]
105+
})cpp";
106+
std::string ConstNamespaceCheckOutput = R"cpp(
107+
namespace X { struct Y { int z; }; }
108+
int extracted(const X::Y &y) {
109+
return y.z + y.z;
110+
}
111+
int f(const X::Y &y) {
112+
return extracted(y);
113+
})cpp";
114+
EXPECT_EQ(apply(ConstNamespaceCheckInput), ConstNamespaceCheckOutput);
115+
100116
// Don't extract when we need to make a function as a parameter.
101117
EXPECT_THAT(apply("void f() { [[int a; f();]] }"), StartsWith("fail"));
102118

clang/docs/OpenCLSupport.rst

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ OpenCL Support
2020
Clang fully supports all OpenCL C versions from 1.1 to 2.0.
2121

2222
Please refer to `Bugzilla
23-
<https://bugs.llvm.org/buglist.cgi?component=OpenCL&list_id=172679&product=clang&resolution=--->`_
23+
<https://bugs.llvm.org/buglist.cgi?component=OpenCL&list_id=172679&product=clang&resolution=--->`__
2424
for the most up to date bug reports.
2525

2626

@@ -45,3 +45,57 @@ Missing features or with limited support
4545
- Initialization of objects in `__constant` address spaces is not guaranteed to work.
4646

4747
- `addrspace_cast` operator is not supported.
48+
49+
Experimental features
50+
=====================
51+
52+
Clang provides the following new WIP features for the developers to experiment
53+
and provide early feedback or contribute with further improvements.
54+
Feel free to contact us on `cfe-dev
55+
<https://lists.llvm.org/mailman/listinfo/cfe-dev>`_ or via `Bugzilla
56+
<https://bugs.llvm.org/>`__.
57+
58+
C++ libraries for OpenCL
59+
------------------------
60+
61+
There is ongoing work to support C++ standard libraries from `LLVM's libcxx
62+
<https://libcxx.llvm.org/>`_ in OpenCL kernel code using C++ for OpenCL mode.
63+
64+
It is currently possible to include `type_traits` from C++17 in the kernel
65+
sources when the following clang extensions are enabled
66+
``__cl_clang_function_pointers`` and ``__cl_clang_variadic_functions``,
67+
see :doc:`LanguageExtensions` for more details. The use of non-conformant
68+
features enabled by the extensions does not expose non-conformant behavior
69+
beyond the compilation i.e. does not get generated in IR or binary.
70+
The extension only appear in metaprogramming
71+
mechanism to identify or verify the properties of types. This allows to provide
72+
the full C++ functionality without a loss of portability. To avoid unsafe use
73+
of the extensions it is recommended that the extensions are disabled directly
74+
after the header include.
75+
76+
**Example of Use**:
77+
78+
The example of kernel code with `type_traits` is illustrated here.
79+
80+
.. code-block:: c++
81+
82+
#pragma OPENCL EXTENSION __cl_clang_function_pointers : enable
83+
#pragma OPENCL EXTENSION __cl_clang_variadic_functions : enable
84+
#include <type_traits>
85+
#pragma OPENCL EXTENSION __cl_clang_function_pointers : disable
86+
#pragma OPENCL EXTENSION __cl_clang_variadic_functions : disable
87+
88+
using sint_type = std::make_signed<unsigned int>::type;
89+
90+
__kernel void foo() {
91+
static_assert(!std::is_same<sint_type, unsigned int>::value);
92+
}
93+
94+
The possible clang invocation to compile the example is as follows:
95+
96+
.. code-block:: console
97+
98+
$ clang -cl-std=clc++ -I<path to libcxx checkout or installation>/include test.cl
99+
100+
Note that `type_traits` is a header only library and therefore no extra
101+
linking step against the standard libraries is required.

clang/include/clang/AST/PrettyPrinter.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace clang {
2020

21+
class DeclContext;
2122
class LangOptions;
2223
class SourceManager;
2324
class Stmt;
@@ -39,6 +40,15 @@ class PrintingCallbacks {
3940
virtual std::string remapPath(StringRef Path) const {
4041
return std::string(Path);
4142
}
43+
44+
/// When printing type to be inserted into code in specific context, this
45+
/// callback can be used to avoid printing the redundant part of the
46+
/// qualifier. For example, when inserting code inside namespace foo, we
47+
/// should print bar::SomeType instead of foo::bar::SomeType.
48+
/// To do this, shouldPrintScope should return true on "foo" NamespaceDecl.
49+
/// The printing stops at the first isScopeVisible() == true, so there will
50+
/// be no calls with outer scopes.
51+
virtual bool isScopeVisible(const DeclContext *DC) const { return false; }
4252
};
4353

4454
/// Describes how types, statements, expressions, and declarations should be

0 commit comments

Comments
 (0)