Skip to content

Commit 25cb138

Browse files
committed
Merge from 'main' to 'sycl-web' (intel#46)
CONFLICT (content): Merge conflict in clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp CONFLICT (content): Merge conflict in clang/test/Driver/clang-offload-wrapper.c
2 parents 69528a4 + 1ffbe8c commit 25cb138

File tree

262 files changed

+7626
-5406
lines changed

Some content is hidden

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

262 files changed

+7626
-5406
lines changed

.mailmap

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ Jon Roelofs <[email protected]> <[email protected]>
3232
3333
LLVM GN Syncbot <[email protected]>
3434
Martin Storsjö <[email protected]>
35-
Saleem Abdulrasool <[email protected]> <[email protected]>
35+
Saleem Abdulrasool <[email protected]>

clang-tools-extra/clangd/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ add_clang_library(clangDaemon
8383
HeaderSourceSwitch.cpp
8484
HeuristicResolver.cpp
8585
Hover.cpp
86+
IncludeCleaner.cpp
8687
IncludeFixer.cpp
8788
InlayHints.cpp
8889
JSONTransport.cpp
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//===--- IncludeCleaner.cpp - Unused/Missing Headers Analysis ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "IncludeCleaner.h"
10+
#include "support/Logger.h"
11+
#include "clang/AST/RecursiveASTVisitor.h"
12+
#include "clang/Basic/SourceLocation.h"
13+
14+
namespace clang {
15+
namespace clangd {
16+
namespace {
17+
18+
/// Crawler traverses the AST and feeds in the locations of (sometimes
19+
/// implicitly) used symbols into \p Result.
20+
class ReferencedLocationCrawler
21+
: public RecursiveASTVisitor<ReferencedLocationCrawler> {
22+
public:
23+
ReferencedLocationCrawler(ReferencedLocations &Result) : Result(Result) {}
24+
25+
bool VisitDeclRefExpr(DeclRefExpr *DRE) {
26+
add(DRE->getDecl());
27+
add(DRE->getFoundDecl());
28+
return true;
29+
}
30+
31+
bool VisitMemberExpr(MemberExpr *ME) {
32+
add(ME->getMemberDecl());
33+
add(ME->getFoundDecl().getDecl());
34+
return true;
35+
}
36+
37+
bool VisitTagType(TagType *TT) {
38+
add(TT->getDecl());
39+
return true;
40+
}
41+
42+
bool VisitCXXConstructExpr(CXXConstructExpr *CCE) {
43+
add(CCE->getConstructor());
44+
return true;
45+
}
46+
47+
bool VisitTemplateSpecializationType(TemplateSpecializationType *TST) {
48+
if (isNew(TST)) {
49+
add(TST->getTemplateName().getAsTemplateDecl()); // Primary template.
50+
add(TST->getAsCXXRecordDecl()); // Specialization
51+
}
52+
return true;
53+
}
54+
55+
bool VisitTypedefType(TypedefType *TT) {
56+
add(TT->getDecl());
57+
return true;
58+
}
59+
60+
// Consider types of any subexpression used, even if the type is not named.
61+
// This is helpful in getFoo().bar(), where Foo must be complete.
62+
// FIXME(kirillbobyrev): Should we tweak this? It may not be desirable to
63+
// consider types "used" when they are not directly spelled in code.
64+
bool VisitExpr(Expr *E) {
65+
TraverseType(E->getType());
66+
return true;
67+
}
68+
69+
bool TraverseType(QualType T) {
70+
if (isNew(T.getTypePtrOrNull())) { // don't care about quals
71+
Base::TraverseType(T);
72+
}
73+
return true;
74+
}
75+
76+
bool VisitUsingDecl(UsingDecl *D) {
77+
for (const auto *Shadow : D->shadows()) {
78+
add(Shadow->getTargetDecl());
79+
}
80+
return true;
81+
}
82+
83+
private:
84+
using Base = RecursiveASTVisitor<ReferencedLocationCrawler>;
85+
86+
void add(const Decl *D) {
87+
if (!D || !isNew(D->getCanonicalDecl())) {
88+
return;
89+
}
90+
for (const Decl *Redecl : D->redecls()) {
91+
Result.insert(Redecl->getLocation());
92+
}
93+
}
94+
95+
bool isNew(const void *P) { return P && Visited.insert(P).second; }
96+
97+
ReferencedLocations &Result;
98+
llvm::DenseSet<const void *> Visited;
99+
};
100+
101+
} // namespace
102+
103+
ReferencedLocations findReferencedLocations(ParsedAST &AST) {
104+
ReferencedLocations Result;
105+
ReferencedLocationCrawler Crawler(Result);
106+
Crawler.TraverseAST(AST.getASTContext());
107+
// FIXME(kirillbobyrev): Handle macros.
108+
return Result;
109+
}
110+
111+
} // namespace clangd
112+
} // namespace clang
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===--- IncludeCleaner.h - Unused/Missing Headers Analysis -----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// Include Cleaner is clangd functionality for providing diagnostics for misuse
11+
/// of transitive headers and unused includes. It is inspired by
12+
/// Include-What-You-Use tool (https://include-what-you-use.org/). Our goal is
13+
/// to provide useful warnings in most popular scenarios but not 1:1 exact
14+
/// feature compatibility.
15+
///
16+
/// FIXME(kirillbobyrev): Add support for IWYU pragmas.
17+
/// FIXME(kirillbobyrev): Add support for standard library headers.
18+
///
19+
//===----------------------------------------------------------------------===//
20+
21+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INCLUDE_CLEANER_H
22+
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INCLUDE_CLEANER_H
23+
24+
#include "Headers.h"
25+
#include "ParsedAST.h"
26+
#include "clang/Basic/SourceLocation.h"
27+
#include "llvm/ADT/DenseSet.h"
28+
29+
namespace clang {
30+
namespace clangd {
31+
32+
using ReferencedLocations = llvm::DenseSet<SourceLocation>;
33+
/// Finds locations of all symbols used in the main file.
34+
///
35+
/// Uses RecursiveASTVisitor to go through main file AST and computes all the
36+
/// locations used symbols are coming from. Returned locations may be macro
37+
/// expansions, and are not resolved to their spelling/expansion location. These
38+
/// locations are later used to determine which headers should be marked as
39+
/// "used" and "directly used".
40+
///
41+
/// We use this to compute unused headers, so we:
42+
///
43+
/// - cover the whole file in a single traversal for efficiency
44+
/// - don't attempt to describe where symbols were referenced from in
45+
/// ambiguous cases (e.g. implicitly used symbols, multiple declarations)
46+
/// - err on the side of reporting all possible locations
47+
ReferencedLocations findReferencedLocations(ParsedAST &AST);
48+
49+
} // namespace clangd
50+
} // namespace clang
51+
52+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INCLUDE_CLEANER_H

clang-tools-extra/clangd/unittests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ add_unittest(ClangdUnitTests ClangdTests
5858
HeadersTests.cpp
5959
HeaderSourceSwitchTests.cpp
6060
HoverTests.cpp
61+
IncludeCleanerTests.cpp
6162
IndexActionTests.cpp
6263
IndexTests.cpp
6364
InlayHintTests.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
//===--- IncludeCleanerTests.cpp --------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "Annotations.h"
10+
#include "IncludeCleaner.h"
11+
#include "TestTU.h"
12+
#include "gmock/gmock.h"
13+
#include "gtest/gtest.h"
14+
15+
namespace clang {
16+
namespace clangd {
17+
namespace {
18+
19+
TEST(IncludeCleaner, ReferencedLocations) {
20+
struct TestCase {
21+
std::string HeaderCode;
22+
std::string MainCode;
23+
};
24+
TestCase Cases[] = {
25+
// DeclRefExpr
26+
{
27+
"int ^x();",
28+
"int y = x();",
29+
},
30+
// RecordDecl
31+
{
32+
"class ^X;",
33+
"X *y;",
34+
},
35+
// TypedefType and UsingDecls
36+
{
37+
"using ^Integer = int;",
38+
"Integer x;",
39+
},
40+
{
41+
"namespace ns { struct ^X; struct ^X {}; }",
42+
"using ns::X;",
43+
},
44+
{
45+
"namespace ns { struct X; struct X {}; }",
46+
"using namespace ns;",
47+
},
48+
{
49+
"struct ^A {}; using B = A; using ^C = B;",
50+
"C a;",
51+
},
52+
{
53+
"typedef bool ^Y; template <typename T> struct ^X {};",
54+
"X<Y> x;",
55+
},
56+
{
57+
"struct Foo; struct ^Foo{}; typedef Foo ^Bar;",
58+
"Bar b;",
59+
},
60+
// MemberExpr
61+
{
62+
"struct ^X{int ^a;}; X ^foo();",
63+
"int y = foo().a;",
64+
},
65+
// Expr (type is traversed)
66+
{
67+
"class ^X{}; X ^foo();",
68+
"auto bar() { return foo(); }",
69+
},
70+
// Redecls
71+
{
72+
"class ^X; class ^X{}; class ^X;",
73+
"X *y;",
74+
},
75+
// Constructor
76+
{
77+
"struct ^X { ^X(int) {} int ^foo(); };",
78+
"auto x = X(42); auto y = x.foo();",
79+
},
80+
// Static function
81+
{
82+
"struct ^X { static bool ^foo(); }; bool X::^foo() {}",
83+
"auto b = X::foo();",
84+
},
85+
// TemplateRecordDecl
86+
{
87+
"template <typename> class ^X;",
88+
"X<int> *y;",
89+
},
90+
// Type name not spelled out in code
91+
{
92+
"class ^X{}; X ^getX();",
93+
"auto x = getX();",
94+
},
95+
// Enums
96+
{
97+
"enum ^Color { ^Red = 42, Green = 9000};",
98+
"int MyColor = Red;",
99+
},
100+
{
101+
"struct ^X { enum ^Language { ^CXX = 42, Python = 9000}; };",
102+
"int Lang = X::CXX;",
103+
},
104+
{
105+
// When a type is resolved via a using declaration, the
106+
// UsingShadowDecl is not referenced in the AST.
107+
// Compare to TypedefType, or DeclRefExpr::getFoundDecl().
108+
// ^
109+
"namespace ns { class ^X; }; using ns::X;",
110+
"X *y;",
111+
}};
112+
for (const TestCase &T : Cases) {
113+
TestTU TU;
114+
TU.Code = T.MainCode;
115+
Annotations Header(T.HeaderCode);
116+
TU.HeaderCode = Header.code().str();
117+
auto AST = TU.build();
118+
119+
std::vector<Position> Points;
120+
for (const auto &Loc : findReferencedLocations(AST)) {
121+
if (AST.getSourceManager().getBufferName(Loc).endswith(
122+
TU.HeaderFilename)) {
123+
Points.push_back(offsetToPosition(
124+
TU.HeaderCode, AST.getSourceManager().getFileOffset(Loc)));
125+
}
126+
}
127+
llvm::sort(Points);
128+
129+
EXPECT_EQ(Points, Header.points()) << T.HeaderCode << "\n---\n"
130+
<< T.MainCode;
131+
}
132+
}
133+
134+
} // namespace
135+
} // namespace clangd
136+
} // namespace clang

clang/include/clang/Basic/CharInfo.h

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ LLVM_READNONE inline bool isASCII(char c) {
4343
return static_cast<unsigned char>(c) <= 127;
4444
}
4545

46+
LLVM_READNONE inline bool isASCII(unsigned char c) { return c <= 127; }
47+
48+
/// Returns true if this is an ASCII character.
49+
LLVM_READNONE inline bool isASCII(uint32_t c) { return c <= 127; }
50+
4651
/// Returns true if this is a valid first character of a C identifier,
4752
/// which is [a-zA-Z_].
4853
LLVM_READONLY inline bool isIdentifierHead(unsigned char c,

clang/include/clang/Basic/DiagnosticLexKinds.td

+4-5
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ def warn_four_char_character_literal : Warning<
113113
// Unicode and UCNs
114114
def err_invalid_utf8 : Error<
115115
"source file is not valid UTF-8">;
116-
def err_non_ascii : Error<
117-
"non-ASCII characters are not allowed outside of literals and identifiers">;
116+
def err_character_not_allowed : Error<
117+
"unexpected character <U+%0>">;
118+
def err_character_not_allowed_identifier : Error<
119+
"character <U+%0> not allowed %select{in|at the start of}1 an identifier">;
118120
def ext_unicode_whitespace : ExtWarn<
119121
"treating Unicode character as whitespace">,
120122
InGroup<DiagGroup<"unicode-whitespace">>;
@@ -150,9 +152,6 @@ def warn_c99_compat_unicode_id : Warning<
150152
"%select{using this character in an identifier|starting an identifier with "
151153
"this character}0 is incompatible with C99">,
152154
InGroup<C99Compat>, DefaultIgnore;
153-
def warn_cxx98_compat_unicode_id : Warning<
154-
"using this character in an identifier is incompatible with C++98">,
155-
InGroup<CXX98Compat>, DefaultIgnore;
156155

157156
def warn_cxx98_compat_literal_ucn_escape_basic_scs : Warning<
158157
"specifying character '%0' with a universal character name "

clang/include/clang/Basic/LangStandards.def

+7
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,26 @@ LANGSTANDARD(opencl20, "cl2.0",
180180
LANGSTANDARD(opencl30, "cl3.0",
181181
OpenCL, "OpenCL 3.0",
182182
LineComment | C99 | Digraphs | HexFloat | OpenCL)
183+
183184
LANGSTANDARD(openclcpp10, "clc++1.0",
184185
OpenCL, "C++ for OpenCL 1.0",
185186
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
186187
Digraphs | HexFloat | OpenCL)
187188
LANGSTANDARD_ALIAS(openclcpp10, "clc++")
188189

190+
LANGSTANDARD(openclcpp2021, "clc++2021",
191+
OpenCL, "C++ for OpenCL 2021",
192+
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
193+
Digraphs | HexFloat | OpenCL)
194+
189195
LANGSTANDARD_ALIAS_DEPR(opencl10, "CL")
190196
LANGSTANDARD_ALIAS_DEPR(opencl11, "CL1.1")
191197
LANGSTANDARD_ALIAS_DEPR(opencl12, "CL1.2")
192198
LANGSTANDARD_ALIAS_DEPR(opencl20, "CL2.0")
193199
LANGSTANDARD_ALIAS_DEPR(opencl30, "CL3.0")
194200
LANGSTANDARD_ALIAS_DEPR(openclcpp10, "CLC++")
195201
LANGSTANDARD_ALIAS_DEPR(openclcpp10, "CLC++1.0")
202+
LANGSTANDARD_ALIAS_DEPR(openclcpp2021, "CLC++2021")
196203

197204
// CUDA
198205
LANGSTANDARD(cuda, "cuda", CUDA, "NVIDIA CUDA(tm)",

clang/include/clang/Driver/Options.td

+1-1
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ def cl_no_signed_zeros : Flag<["-"], "cl-no-signed-zeros">, Group<opencl_Group>,
887887
MarshallingInfoFlag<LangOpts<"CLNoSignedZero">>;
888888
def cl_std_EQ : Joined<["-"], "cl-std=">, Group<opencl_Group>, Flags<[CC1Option]>,
889889
HelpText<"OpenCL language standard to compile for.">,
890-
Values<"cl,CL,cl1.0,CL1.0,cl1.1,CL1.1,cl1.2,CL1.2,cl2.0,CL2.0,cl3.0,CL3.0,clc++,CLC++,clc++1.0,CLC++1.0">;
890+
Values<"cl,CL,cl1.0,CL1.0,cl1.1,CL1.1,cl1.2,CL1.2,cl2.0,CL2.0,cl3.0,CL3.0,clc++,CLC++,clc++1.0,CLC++1.0,clc++2021,CLC++2021">;
891891
def cl_denorms_are_zero : Flag<["-"], "cl-denorms-are-zero">, Group<opencl_Group>,
892892
HelpText<"OpenCL only. Allow denormals to be flushed to zero.">;
893893
def cl_fp32_correctly_rounded_divide_sqrt : Flag<["-"], "cl-fp32-correctly-rounded-divide-sqrt">, Group<opencl_Group>, Flags<[CC1Option]>,

0 commit comments

Comments
 (0)