Skip to content

Commit 1db4a5d

Browse files
committed
Merge branch 'master' into intel
Signed-off-by: Dmitry Sidorov <[email protected]>
2 parents 83d6539 + f672b61 commit 1db4a5d

File tree

4,480 files changed

+99437
-410493
lines changed

Some content is hidden

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

4,480 files changed

+99437
-410493
lines changed

clang-tools-extra/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
include(CMakeDependentOption)
2+
13
add_subdirectory(clang-apply-replacements)
24
add_subdirectory(clang-reorder-fields)
35
add_subdirectory(modularize)
@@ -9,7 +11,6 @@ add_subdirectory(clang-doc)
911
add_subdirectory(clang-include-fixer)
1012
add_subdirectory(clang-move)
1113
add_subdirectory(clang-query)
12-
add_subdirectory(clangd)
1314
add_subdirectory(pp-trace)
1415
add_subdirectory(tool-template)
1516

@@ -25,3 +26,9 @@ if( CLANG_TOOLS_EXTRA_INCLUDE_DOCS )
2526
add_subdirectory(docs)
2627
endif()
2728

29+
# clangd has its own CMake tree. It requires threads.
30+
CMAKE_DEPENDENT_OPTION(CLANG_ENABLE_CLANGD "Build clangd language server" ON
31+
"LLVM_ENABLE_THREADS" OFF)
32+
if (CLANG_ENABLE_CLANGD)
33+
add_subdirectory(clangd)
34+
endif()

clang-tools-extra/clang-tidy/add_new_check.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def adapt_cmake(module_path, check_name_camel):
4646

4747

4848
# Adds a header for the new check.
49-
def write_header(module_path, module, check_name, check_name_camel):
49+
def write_header(module_path, module, namespace, check_name, check_name_camel):
5050
check_name_dashes = module + '-' + check_name
5151
filename = os.path.join(module_path, check_name_camel) + '.h'
5252
print('Creating %s...' % filename)
@@ -73,7 +73,7 @@ def write_header(module_path, module, check_name, check_name_camel):
7373
7474
namespace clang {
7575
namespace tidy {
76-
namespace %(module)s {
76+
namespace %(namespace)s {
7777
7878
/// FIXME: Write a short description.
7979
///
@@ -87,19 +87,20 @@ class %(check_name)s : public ClangTidyCheck {
8787
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
8888
};
8989
90-
} // namespace %(module)s
90+
} // namespace %(namespace)s
9191
} // namespace tidy
9292
} // namespace clang
9393
9494
#endif // %(header_guard)s
9595
""" % {'header_guard': header_guard,
9696
'check_name': check_name_camel,
9797
'check_name_dashes': check_name_dashes,
98-
'module': module})
98+
'module': module,
99+
'namespace': namespace})
99100

100101

101102
# Adds the implementation of the new check.
102-
def write_implementation(module_path, module, check_name_camel):
103+
def write_implementation(module_path, module, namespace, check_name_camel):
103104
filename = os.path.join(module_path, check_name_camel) + '.cpp'
104105
print('Creating %s...' % filename)
105106
with open(filename, 'w') as f:
@@ -124,7 +125,7 @@ def write_implementation(module_path, module, check_name_camel):
124125
125126
namespace clang {
126127
namespace tidy {
127-
namespace %(module)s {
128+
namespace %(namespace)s {
128129
129130
void %(check_name)s::registerMatchers(MatchFinder *Finder) {
130131
// FIXME: Add matchers.
@@ -142,11 +143,12 @@ def write_implementation(module_path, module, check_name_camel):
142143
<< FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
143144
}
144145
145-
} // namespace %(module)s
146+
} // namespace %(namespace)s
146147
} // namespace tidy
147148
} // namespace clang
148149
""" % {'check_name': check_name_camel,
149-
'module': module})
150+
'module': module,
151+
'namespace': namespace})
150152

151153

152154
# Modifies the module to include the new check.
@@ -375,8 +377,15 @@ def main():
375377

376378
if not adapt_cmake(module_path, check_name_camel):
377379
return
378-
write_header(module_path, module, check_name, check_name_camel)
379-
write_implementation(module_path, module, check_name_camel)
380+
381+
# Map module names to namespace names that don't conflict with widely used top-level namespaces.
382+
if module == 'llvm':
383+
namespace = module + '_check'
384+
else:
385+
namespace = module
386+
387+
write_header(module_path, module, namespace, check_name, check_name_camel)
388+
write_implementation(module_path, module, namespace, check_name_camel)
380389
adapt_module(module_path, module, check_name, check_name_camel)
381390
add_release_notes(module_path, module, check_name)
382391
test_extension = language_to_extension.get(args.language)
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
//===--- BranchCloneCheck.cpp - clang-tidy --------------------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "BranchCloneCheck.h"
11+
#include "clang/AST/ASTContext.h"
12+
#include "clang/ASTMatchers/ASTMatchFinder.h"
13+
#include "clang/Analysis/CloneDetection.h"
14+
#include "llvm/Support/Casting.h"
15+
16+
using namespace clang;
17+
using namespace clang::ast_matchers;
18+
19+
/// Returns true when the statements are Type I clones of each other.
20+
static bool areStatementsIdentical(const Stmt *LHS, const Stmt *RHS,
21+
const ASTContext &Context) {
22+
llvm::FoldingSetNodeID DataLHS, DataRHS;
23+
LHS->Profile(DataLHS, Context, false);
24+
RHS->Profile(DataRHS, Context, false);
25+
return (DataLHS == DataRHS);
26+
}
27+
28+
namespace {
29+
/// A branch in a switch may consist of several statements; while a branch in
30+
/// an if/else if/else chain is one statement (which may be a CompoundStmt).
31+
using SwitchBranch = llvm::SmallVector<const Stmt *, 2>;
32+
} // anonymous namespace
33+
34+
/// Determines if the bodies of two branches in a switch statements are Type I
35+
/// clones of each other. This function only examines the body of the branch
36+
/// and ignores the `case X:` or `default:` at the start of the branch.
37+
static bool areSwitchBranchesIdentical(const SwitchBranch LHS,
38+
const SwitchBranch RHS,
39+
const ASTContext &Context) {
40+
if (LHS.size() != RHS.size())
41+
return false;
42+
43+
for (size_t i = 0, Size = LHS.size(); i < Size; i++) {
44+
// NOTE: We strip goto labels and annotations in addition to stripping
45+
// the `case X:` or `default:` labels, but it is very unlikely that this
46+
// would casue false positives in real-world code.
47+
if (!areStatementsIdentical(LHS[i]->stripLabelLikeStatements(),
48+
RHS[i]->stripLabelLikeStatements(), Context)) {
49+
return false;
50+
}
51+
}
52+
53+
return true;
54+
}
55+
56+
namespace clang {
57+
namespace tidy {
58+
namespace bugprone {
59+
60+
void BranchCloneCheck::registerMatchers(MatchFinder *Finder) {
61+
Finder->addMatcher(
62+
ifStmt(stmt().bind("if"),
63+
hasParent(stmt(unless(ifStmt(hasElse(equalsBoundNode("if")))))),
64+
hasElse(stmt().bind("else"))),
65+
this);
66+
Finder->addMatcher(switchStmt().bind("switch"), this);
67+
Finder->addMatcher(conditionalOperator().bind("condOp"), this);
68+
}
69+
70+
void BranchCloneCheck::check(const MatchFinder::MatchResult &Result) {
71+
const ASTContext &Context = *Result.Context;
72+
73+
if (const auto *IS = Result.Nodes.getNodeAs<IfStmt>("if")) {
74+
const Stmt *Then = IS->getThen();
75+
assert(Then && "An IfStmt must have a `then` branch!");
76+
77+
const Stmt *Else = Result.Nodes.getNodeAs<Stmt>("else");
78+
assert(Else && "We only look for `if` statements with an `else` branch!");
79+
80+
if (!isa<IfStmt>(Else)) {
81+
// Just a simple if with no `else if` branch.
82+
if (areStatementsIdentical(Then->IgnoreContainers(),
83+
Else->IgnoreContainers(), Context)) {
84+
diag(IS->getBeginLoc(), "if with identical then and else branches");
85+
diag(IS->getElseLoc(), "else branch starts here", DiagnosticIDs::Note);
86+
}
87+
return;
88+
}
89+
90+
// This is the complicated case when we start an if/else if/else chain.
91+
// To find all the duplicates, we collect all the branches into a vector.
92+
llvm::SmallVector<const Stmt *, 4> Branches;
93+
const IfStmt *Cur = IS;
94+
while (true) {
95+
// Store the `then` branch.
96+
Branches.push_back(Cur->getThen());
97+
98+
Else = Cur->getElse();
99+
// The chain ends if there is no `else` branch.
100+
if (!Else)
101+
break;
102+
103+
// Check if there is another `else if`...
104+
Cur = dyn_cast<IfStmt>(Else);
105+
if (!Cur) {
106+
// ...this is just a plain `else` branch at the end of the chain.
107+
Branches.push_back(Else);
108+
break;
109+
}
110+
}
111+
112+
size_t N = Branches.size();
113+
llvm::BitVector KnownAsClone(N);
114+
115+
for (size_t i = 0; i + 1 < N; i++) {
116+
// We have already seen Branches[i] as a clone of an earlier branch.
117+
if (KnownAsClone[i])
118+
continue;
119+
120+
int NumCopies = 1;
121+
122+
for (size_t j = i + 1; j < N; j++) {
123+
if (KnownAsClone[j] ||
124+
!areStatementsIdentical(Branches[i]->IgnoreContainers(),
125+
Branches[j]->IgnoreContainers(), Context))
126+
continue;
127+
128+
NumCopies++;
129+
KnownAsClone[j] = true;
130+
131+
if (NumCopies == 2) {
132+
// We report the first occurence only when we find the second one.
133+
diag(Branches[i]->getBeginLoc(),
134+
"repeated branch in conditional chain");
135+
diag(Lexer::getLocForEndOfToken(Branches[i]->getEndLoc(), 0,
136+
*Result.SourceManager, getLangOpts()),
137+
"end of the original", DiagnosticIDs::Note);
138+
}
139+
140+
diag(Branches[j]->getBeginLoc(), "clone %0 starts here",
141+
DiagnosticIDs::Note)
142+
<< (NumCopies - 1);
143+
}
144+
}
145+
return;
146+
}
147+
148+
if (const auto *CO = Result.Nodes.getNodeAs<ConditionalOperator>("condOp")) {
149+
// We do not try to detect chains of ?: operators.
150+
if (areStatementsIdentical(CO->getTrueExpr(), CO->getFalseExpr(), Context))
151+
diag(CO->getQuestionLoc(),
152+
"conditional operator with identical true and false expressions");
153+
154+
return;
155+
}
156+
157+
if (const auto *SS = Result.Nodes.getNodeAs<SwitchStmt>("switch")) {
158+
const CompoundStmt *Body = dyn_cast_or_null<CompoundStmt>(SS->getBody());
159+
160+
// Code like
161+
// switch (x) case 0: case 1: foobar();
162+
// is legal and calls foobar() if and only if x is either 0 or 1;
163+
// but we do not try to distinguish branches in such code.
164+
if (!Body)
165+
return;
166+
167+
// We will first collect the branches of the switch statements. For the
168+
// sake of simplicity we say that branches are delimited by the SwitchCase
169+
// (`case:` or `default:`) children of Body; that is, we ignore `case:` or
170+
// `default:` labels embedded inside other statements and we do not follow
171+
// the effects of `break` and other manipulation of the control-flow.
172+
llvm::SmallVector<SwitchBranch, 4> Branches;
173+
for (const Stmt *S : Body->body()) {
174+
// If this is a `case` or `default`, we start a new, empty branch.
175+
if (isa<SwitchCase>(S))
176+
Branches.emplace_back();
177+
178+
// There may be code before the first branch (which can be dead code
179+
// and can be code reached either through goto or through case labels
180+
// that are embedded inside e.g. inner compound statements); we do not
181+
// store those statements in branches.
182+
if (!Branches.empty())
183+
Branches.back().push_back(S);
184+
}
185+
186+
auto End = Branches.end();
187+
auto BeginCurrent = Branches.begin();
188+
while (BeginCurrent < End) {
189+
auto EndCurrent = BeginCurrent + 1;
190+
while (EndCurrent < End &&
191+
areSwitchBranchesIdentical(*BeginCurrent, *EndCurrent, Context)) {
192+
++EndCurrent;
193+
}
194+
// At this point the iterator range {BeginCurrent, EndCurrent} contains a
195+
// complete family of consecutive identical branches.
196+
if (EndCurrent > BeginCurrent + 1) {
197+
diag(BeginCurrent->front()->getBeginLoc(),
198+
"switch has %0 consecutive identical branches")
199+
<< static_cast<int>(std::distance(BeginCurrent, EndCurrent));
200+
201+
SourceLocation EndLoc = (EndCurrent - 1)->back()->getEndLoc();
202+
// If the case statement is generated from a macro, it's SourceLocation
203+
// may be invalid, resuling in an assertation failure down the line.
204+
// While not optimal, try the begin location in this case, it's still
205+
// better then nothing.
206+
if (EndLoc.isInvalid())
207+
EndLoc = (EndCurrent - 1)->back()->getBeginLoc();
208+
209+
if (EndLoc.isMacroID())
210+
EndLoc = Context.getSourceManager().getExpansionLoc(EndLoc);
211+
212+
diag(Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
213+
getLangOpts()),
214+
"last of these clones ends here", DiagnosticIDs::Note);
215+
}
216+
BeginCurrent = EndCurrent;
217+
}
218+
return;
219+
}
220+
221+
llvm_unreachable("No if statement and no switch statement.");
222+
}
223+
224+
} // namespace bugprone
225+
} // namespace tidy
226+
} // namespace clang
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===--- BranchCloneCheck.h - clang-tidy ------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BRANCHCLONECHECK_H
11+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BRANCHCLONECHECK_H
12+
13+
#include "../ClangTidy.h"
14+
15+
namespace clang {
16+
namespace tidy {
17+
namespace bugprone {
18+
19+
/// A check for detecting if/else if/else chains where two or more branches are
20+
/// Type I clones of each other (that is, they contain identical code), for
21+
/// detecting switch statements where two or more consecutive branches are
22+
/// Type I clones of each other, and for detecting conditional operators where
23+
/// the true and false expressions are Type I clones of each other.
24+
///
25+
/// For the user-facing documentation see:
26+
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-branch-clone.html
27+
class BranchCloneCheck : public ClangTidyCheck {
28+
public:
29+
BranchCloneCheck(StringRef Name, ClangTidyContext *Context)
30+
: ClangTidyCheck(Name, Context) {}
31+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
32+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
33+
};
34+
35+
} // namespace bugprone
36+
} // namespace tidy
37+
} // namespace clang
38+
39+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BRANCHCLONECHECK_H

0 commit comments

Comments
 (0)