Skip to content

Commit 5658945

Browse files
committed
Merge from 'master' to 'sycl-web' (#3)
CONFLICT (content): Merge conflict in clang/lib/Sema/SemaSYCL.cpp CONFLICT (content): Merge conflict in clang/include/clang/Sema/Sema.h
2 parents 15de32a + e39da8a commit 5658945

File tree

1,748 files changed

+57140
-18927
lines changed

Some content is hidden

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

1,748 files changed

+57140
-18927
lines changed

.git-blame-ignore-revs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ d8f0e6caa91e230a486c948ab643174e40bdf215
3434

3535
# Cleanup __config indention. NFC.
3636
2b772b930e097ed6f06d698a51e291c7fd318baa
37+
38+
# Fixing whitespace problems
39+
94b2dd0998230c758abd92c99d3700c971f7a31a
40+
41+
# Wiped out some non-ascii characters that snuck into the copyright.
42+
5b08a8a43254ed30bd953e869b0fd9fc1e8b82d0

clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_clang_library(clangTidyCppCoreGuidelinesModule
1313
NarrowingConversionsCheck.cpp
1414
NoMallocCheck.cpp
1515
OwningMemoryCheck.cpp
16+
PreferMemberInitializerCheck.cpp
1617
ProBoundsArrayToPointerDecayCheck.cpp
1718
ProBoundsConstantArrayIndexCheck.cpp
1819
ProBoundsPointerArithmeticCheck.cpp

clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "NarrowingConversionsCheck.h"
2323
#include "NoMallocCheck.h"
2424
#include "OwningMemoryCheck.h"
25+
#include "PreferMemberInitializerCheck.h"
2526
#include "ProBoundsArrayToPointerDecayCheck.h"
2627
#include "ProBoundsConstantArrayIndexCheck.h"
2728
#include "ProBoundsPointerArithmeticCheck.h"
@@ -66,6 +67,8 @@ class CppCoreGuidelinesModule : public ClangTidyModule {
6667
"cppcoreguidelines-non-private-member-variables-in-classes");
6768
CheckFactories.registerCheck<OwningMemoryCheck>(
6869
"cppcoreguidelines-owning-memory");
70+
CheckFactories.registerCheck<PreferMemberInitializerCheck>(
71+
"cppcoreguidelines-prefer-member-initializer");
6972
CheckFactories.registerCheck<ProBoundsArrayToPointerDecayCheck>(
7073
"cppcoreguidelines-pro-bounds-array-to-pointer-decay");
7174
CheckFactories.registerCheck<ProBoundsConstantArrayIndexCheck>(
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
//===--- PreferMemberInitializerCheck.cpp - clang-tidy -------------------===//
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 "PreferMemberInitializerCheck.h"
10+
#include "clang/AST/ASTContext.h"
11+
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
#include "clang/Lex/Lexer.h"
13+
14+
using namespace clang::ast_matchers;
15+
16+
namespace clang {
17+
namespace tidy {
18+
namespace cppcoreguidelines {
19+
20+
static bool isControlStatement(const Stmt *S) {
21+
return isa<IfStmt, SwitchStmt, ForStmt, WhileStmt, DoStmt, ReturnStmt,
22+
GotoStmt, CXXTryStmt, CXXThrowExpr>(S);
23+
}
24+
25+
static bool isNoReturnCallStatement(const Stmt *S) {
26+
const auto *Call = dyn_cast<CallExpr>(S);
27+
if (!Call)
28+
return false;
29+
30+
const FunctionDecl *Func = Call->getDirectCallee();
31+
if (!Func)
32+
return false;
33+
34+
return Func->isNoReturn();
35+
}
36+
37+
static bool isLiteral(const Expr *E) {
38+
return isa<StringLiteral, CharacterLiteral, IntegerLiteral, FloatingLiteral,
39+
CXXBoolLiteralExpr, CXXNullPtrLiteralExpr>(E);
40+
}
41+
42+
static bool isUnaryExprOfLiteral(const Expr *E) {
43+
if (const auto *UnOp = dyn_cast<UnaryOperator>(E))
44+
return isLiteral(UnOp->getSubExpr());
45+
return false;
46+
}
47+
48+
static bool shouldBeDefaultMemberInitializer(const Expr *Value) {
49+
if (isLiteral(Value) || isUnaryExprOfLiteral(Value))
50+
return true;
51+
52+
if (const auto *DRE = dyn_cast<DeclRefExpr>(Value))
53+
return isa<EnumConstantDecl>(DRE->getDecl());
54+
55+
return false;
56+
}
57+
58+
static const std::pair<const FieldDecl *, const Expr *>
59+
isAssignmentToMemberOf(const RecordDecl *Rec, const Stmt *S) {
60+
if (const auto *BO = dyn_cast<BinaryOperator>(S)) {
61+
if (BO->getOpcode() != BO_Assign)
62+
return std::make_pair(nullptr, nullptr);
63+
64+
const auto *ME = dyn_cast<MemberExpr>(BO->getLHS()->IgnoreParenImpCasts());
65+
if (!ME)
66+
return std::make_pair(nullptr, nullptr);
67+
68+
const auto *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
69+
if (!Field)
70+
return std::make_pair(nullptr, nullptr);
71+
72+
if (isa<CXXThisExpr>(ME->getBase()))
73+
return std::make_pair(Field, BO->getRHS()->IgnoreParenImpCasts());
74+
} else if (const auto *COCE = dyn_cast<CXXOperatorCallExpr>(S)) {
75+
if (COCE->getOperator() != OO_Equal)
76+
return std::make_pair(nullptr, nullptr);
77+
78+
const auto *ME =
79+
dyn_cast<MemberExpr>(COCE->getArg(0)->IgnoreParenImpCasts());
80+
if (!ME)
81+
return std::make_pair(nullptr, nullptr);
82+
83+
const auto *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
84+
if (!Field)
85+
return std::make_pair(nullptr, nullptr);
86+
87+
if (isa<CXXThisExpr>(ME->getBase()))
88+
return std::make_pair(Field, COCE->getArg(1)->IgnoreParenImpCasts());
89+
}
90+
91+
return std::make_pair(nullptr, nullptr);
92+
}
93+
94+
PreferMemberInitializerCheck::PreferMemberInitializerCheck(
95+
StringRef Name, ClangTidyContext *Context)
96+
: ClangTidyCheck(Name, Context),
97+
IsUseDefaultMemberInitEnabled(
98+
Context->isCheckEnabled("modernize-use-default-member-init")),
99+
UseAssignment(OptionsView("modernize-use-default-member-init",
100+
Context->getOptions().CheckOptions)
101+
.get("UseAssignment", false)) {}
102+
103+
void PreferMemberInitializerCheck::storeOptions(
104+
ClangTidyOptions::OptionMap &Opts) {
105+
Options.store(Opts, "UseAssignment", UseAssignment);
106+
}
107+
108+
void PreferMemberInitializerCheck::registerMatchers(MatchFinder *Finder) {
109+
Finder->addMatcher(
110+
cxxConstructorDecl(hasBody(compoundStmt()), unless(isInstantiated()))
111+
.bind("ctor"),
112+
this);
113+
}
114+
115+
void PreferMemberInitializerCheck::check(
116+
const MatchFinder::MatchResult &Result) {
117+
const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
118+
const auto *Body = cast<CompoundStmt>(Ctor->getBody());
119+
120+
const CXXRecordDecl *Class = Ctor->getParent();
121+
SourceLocation InsertPos;
122+
bool FirstToCtorInits = true;
123+
124+
for (const Stmt *S : Body->body()) {
125+
if (S->getBeginLoc().isMacroID()) {
126+
StringRef MacroName =
127+
Lexer::getImmediateMacroName(S->getBeginLoc(), *Result.SourceManager,
128+
getLangOpts());
129+
if (MacroName.contains_lower("assert"))
130+
return;
131+
}
132+
if (isControlStatement(S))
133+
return;
134+
135+
if (isNoReturnCallStatement(S))
136+
return;
137+
138+
if (const auto *CondOp = dyn_cast<ConditionalOperator>(S)) {
139+
if (isNoReturnCallStatement(CondOp->getLHS()) ||
140+
isNoReturnCallStatement(CondOp->getRHS()))
141+
return;
142+
}
143+
144+
const FieldDecl *Field;
145+
const Expr *InitValue;
146+
std::tie(Field, InitValue) = isAssignmentToMemberOf(Class, S);
147+
if (Field) {
148+
if (IsUseDefaultMemberInitEnabled && getLangOpts().CPlusPlus11 &&
149+
Ctor->isDefaultConstructor() &&
150+
(getLangOpts().CPlusPlus20 || !Field->isBitField()) &&
151+
(!isa<RecordDecl>(Class->getDeclContext()) ||
152+
!cast<RecordDecl>(Class->getDeclContext())->isUnion()) &&
153+
shouldBeDefaultMemberInitializer(InitValue)) {
154+
auto Diag =
155+
diag(S->getBeginLoc(), "%0 should be initialized in an in-class"
156+
" default member initializer")
157+
<< Field;
158+
159+
SourceLocation FieldEnd =
160+
Lexer::getLocForEndOfToken(Field->getSourceRange().getEnd(), 0,
161+
*Result.SourceManager, getLangOpts());
162+
Diag << FixItHint::CreateInsertion(FieldEnd,
163+
UseAssignment ? " = " : "{")
164+
<< FixItHint::CreateInsertionFromRange(
165+
FieldEnd,
166+
CharSourceRange(InitValue->getSourceRange(), true))
167+
<< FixItHint::CreateInsertion(FieldEnd, UseAssignment ? "" : "}");
168+
169+
SourceLocation SemiColonEnd =
170+
Lexer::findNextToken(S->getEndLoc(), *Result.SourceManager,
171+
getLangOpts())
172+
->getEndLoc();
173+
CharSourceRange StmtRange =
174+
CharSourceRange::getCharRange(S->getBeginLoc(), SemiColonEnd);
175+
176+
Diag << FixItHint::CreateRemoval(StmtRange);
177+
} else {
178+
auto Diag =
179+
diag(S->getBeginLoc(), "%0 should be initialized in a member"
180+
" initializer of the constructor")
181+
<< Field;
182+
183+
bool AddComma = false;
184+
if (!Ctor->getNumCtorInitializers() && FirstToCtorInits) {
185+
SourceLocation BodyPos = Ctor->getBody()->getBeginLoc();
186+
SourceLocation NextPos = Ctor->getBeginLoc();
187+
do {
188+
InsertPos = NextPos;
189+
NextPos = Lexer::findNextToken(NextPos, *Result.SourceManager,
190+
getLangOpts())
191+
->getLocation();
192+
} while (NextPos != BodyPos);
193+
InsertPos = Lexer::getLocForEndOfToken(
194+
InsertPos, 0, *Result.SourceManager, getLangOpts());
195+
196+
Diag << FixItHint::CreateInsertion(InsertPos, " : ");
197+
} else {
198+
bool Found = false;
199+
for (const auto *Init : Ctor->inits()) {
200+
if (Init->isMemberInitializer()) {
201+
if (Result.SourceManager->isBeforeInTranslationUnit(
202+
Field->getLocation(), Init->getMember()->getLocation())) {
203+
InsertPos = Init->getSourceLocation();
204+
Found = true;
205+
break;
206+
}
207+
}
208+
}
209+
210+
if (!Found) {
211+
if (Ctor->getNumCtorInitializers()) {
212+
InsertPos = Lexer::getLocForEndOfToken(
213+
(*Ctor->init_rbegin())->getSourceRange().getEnd(), 0,
214+
*Result.SourceManager, getLangOpts());
215+
}
216+
Diag << FixItHint::CreateInsertion(InsertPos, ", ");
217+
} else {
218+
AddComma = true;
219+
}
220+
}
221+
Diag << FixItHint::CreateInsertion(InsertPos, Field->getName())
222+
<< FixItHint::CreateInsertion(InsertPos, "(")
223+
<< FixItHint::CreateInsertionFromRange(
224+
InsertPos,
225+
CharSourceRange(InitValue->getSourceRange(), true))
226+
<< FixItHint::CreateInsertion(InsertPos, ")");
227+
if (AddComma)
228+
Diag << FixItHint::CreateInsertion(InsertPos, ", ");
229+
230+
SourceLocation SemiColonEnd =
231+
Lexer::findNextToken(S->getEndLoc(), *Result.SourceManager,
232+
getLangOpts())
233+
->getEndLoc();
234+
CharSourceRange StmtRange =
235+
CharSourceRange::getCharRange(S->getBeginLoc(), SemiColonEnd);
236+
237+
Diag << FixItHint::CreateRemoval(StmtRange);
238+
FirstToCtorInits = false;
239+
}
240+
}
241+
}
242+
}
243+
244+
} // namespace cppcoreguidelines
245+
} // namespace tidy
246+
} // namespace clang
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===--- PreferMemberInitializerCheck.h - clang-tidy ------------*- 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+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PREFERMEMBERINITIALIZERCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PREFERMEMBERINITIALIZERCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang {
15+
namespace tidy {
16+
namespace cppcoreguidelines {
17+
18+
/// Finds member initializations in the constructor body which can be placed
19+
/// into the initialization list instead.
20+
///
21+
/// For the user-facing documentation see:
22+
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-prefer-member-initializer.html
23+
class PreferMemberInitializerCheck : public ClangTidyCheck {
24+
public:
25+
PreferMemberInitializerCheck(StringRef Name, ClangTidyContext *Context);
26+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
27+
return LangOpts.CPlusPlus;
28+
}
29+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
30+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
31+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
32+
33+
const bool IsUseDefaultMemberInitEnabled;
34+
const bool UseAssignment;
35+
};
36+
37+
} // namespace cppcoreguidelines
38+
} // namespace tidy
39+
} // namespace clang
40+
41+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PREFERMEMBERINITIALIZERCHECK_H

clang-tools-extra/clangd/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ set(LLVM_LINK_COMPONENTS
2828
FrontendOpenMP
2929
Option
3030
)
31+
32+
include(${CMAKE_CURRENT_SOURCE_DIR}/quality/CompletionModel.cmake)
33+
gen_decision_forest(${CMAKE_CURRENT_SOURCE_DIR}/quality/model CompletionModel clang::clangd::Example)
3134

3235
if(MSVC AND NOT CLANG_CL)
3336
set_source_files_properties(CompileCommands.cpp PROPERTIES COMPILE_FLAGS -wd4130) # disables C4130: logical operation on address of string constant
@@ -77,6 +80,7 @@ add_clang_library(clangDaemon
7780
TUScheduler.cpp
7881
URI.cpp
7982
XRefs.cpp
83+
${CMAKE_CURRENT_BINARY_DIR}/CompletionModel.cpp
8084

8185
index/Background.cpp
8286
index/BackgroundIndexLoader.cpp
@@ -117,6 +121,11 @@ add_clang_library(clangDaemon
117121
omp_gen
118122
)
119123

124+
# Include generated CompletionModel headers.
125+
target_include_directories(clangDaemon PUBLIC
126+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
127+
)
128+
120129
clang_target_link_libraries(clangDaemon
121130
PRIVATE
122131
clangAST

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,10 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
247247
void (ClangdLSPServer::*Handler)(const Param &, Callback<Result>)) {
248248
Calls[Method] = [Method, Handler, this](llvm::json::Value RawParams,
249249
ReplyOnce Reply) {
250-
Param P;
251-
if (fromJSON(RawParams, P)) {
252-
(Server.*Handler)(P, std::move(Reply));
253-
} else {
254-
elog("Failed to decode {0} request.", Method);
255-
Reply(llvm::make_error<LSPError>("failed to decode request",
256-
ErrorCode::InvalidRequest));
257-
}
250+
auto P = parse<Param>(RawParams, Method, "request");
251+
if (!P)
252+
return Reply(P.takeError());
253+
(Server.*Handler)(*P, std::move(Reply));
258254
};
259255
}
260256

@@ -292,14 +288,12 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
292288
void (ClangdLSPServer::*Handler)(const Param &)) {
293289
Notifications[Method] = [Method, Handler,
294290
this](llvm::json::Value RawParams) {
295-
Param P;
296-
if (!fromJSON(RawParams, P)) {
297-
elog("Failed to decode {0} request.", Method);
298-
return;
299-
}
291+
llvm::Expected<Param> P = parse<Param>(RawParams, Method, "request");
292+
if (!P)
293+
return llvm::consumeError(P.takeError());
300294
trace::Span Tracer(Method, LSPLatency);
301295
SPAN_ATTACH(Tracer, "Params", RawParams);
302-
(Server.*Handler)(P);
296+
(Server.*Handler)(*P);
303297
};
304298
}
305299

0 commit comments

Comments
 (0)