Skip to content

Commit f391bef

Browse files
committed
Merge branch 'upstream' into x86-half-zext
2 parents 0fb3b87 + 2385636 commit f391bef

File tree

2,329 files changed

+92647
-80398
lines changed

Some content is hidden

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

2,329 files changed

+92647
-80398
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
/bolt/ @aaupov @maksfb @rafaelauler @ayermolo @dcci @yota9
132132

133133
# Bazel build system.
134-
/utils/bazel/ @rupprecht @keith
134+
/utils/bazel/ @rupprecht @keith @aaronmondal
135135

136136
# InstallAPI and TextAPI
137137
/llvm/**/TextAPI/ @cyndyishida

.github/workflows/libcxx-build-containers.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ name: Build Docker images for libc++ CI
99

1010
permissions:
1111
contents: read
12-
packages: write
1312

1413
on:
1514
push:

bolt/tools/driver/llvm-bolt.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,24 +173,14 @@ void boltMode(int argc, char **argv) {
173173
}
174174
}
175175

176-
static std::string GetExecutablePath(const char *Argv0) {
177-
SmallString<256> ExecutablePath(Argv0);
178-
// Do a PATH lookup if Argv0 isn't a valid path.
179-
if (!llvm::sys::fs::exists(ExecutablePath))
180-
if (llvm::ErrorOr<std::string> P =
181-
llvm::sys::findProgramByName(ExecutablePath))
182-
ExecutablePath = *P;
183-
return std::string(ExecutablePath);
184-
}
185-
186176
int main(int argc, char **argv) {
187177
// Print a stack trace if we signal out.
188178
sys::PrintStackTraceOnErrorSignal(argv[0]);
189179
PrettyStackTraceProgram X(argc, argv);
190180

191181
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
192182

193-
std::string ToolPath = GetExecutablePath(argv[0]);
183+
std::string ToolPath = llvm::sys::fs::getMainExecutable(argv[0], nullptr);
194184

195185
// Initialize targets and assembly printers/parsers.
196186
llvm::InitializeAllTargetInfos();

clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) {
8282
Finder->addMatcher(
8383
cxxConstructExpr(
8484
hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
85-
hasArgument(0, hasType(qualType(isInteger()))),
85+
argumentCountIs(2), hasArgument(0, hasType(qualType(isInteger()))),
8686
hasArgument(1, hasType(qualType(isInteger()))),
8787
anyOf(
8888
// Detect the expression: string('x', 40);
@@ -102,7 +102,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) {
102102
cxxConstructExpr(
103103
hasDeclaration(cxxConstructorDecl(ofClass(
104104
cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)))))),
105-
hasArgument(0, hasType(CharPtrType)),
105+
argumentCountIs(2), hasArgument(0, hasType(CharPtrType)),
106106
hasArgument(1, hasType(isInteger())),
107107
anyOf(
108108
// Detect the expression: string("...", 0);
@@ -114,7 +114,34 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) {
114114
// Detect the expression: string("lit", 5)
115115
allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")),
116116
hasArgument(1, ignoringParenImpCasts(
117-
integerLiteral().bind("int"))))))
117+
integerLiteral().bind("length"))))))
118+
.bind("constructor"),
119+
this);
120+
121+
// Check the literal string constructor with char pointer, start position and
122+
// length parameters. [i.e. string (const char* s, size_t pos, size_t count);]
123+
Finder->addMatcher(
124+
cxxConstructExpr(
125+
hasDeclaration(cxxConstructorDecl(ofClass(
126+
cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)))))),
127+
argumentCountIs(3), hasArgument(0, hasType(CharPtrType)),
128+
hasArgument(1, hasType(qualType(isInteger()))),
129+
hasArgument(2, hasType(qualType(isInteger()))),
130+
anyOf(
131+
// Detect the expression: string("...", 1, 0);
132+
hasArgument(2, ZeroExpr.bind("empty-string")),
133+
// Detect the expression: string("...", -4, 1);
134+
hasArgument(1, NegativeExpr.bind("negative-pos")),
135+
// Detect the expression: string("...", 0, -4);
136+
hasArgument(2, NegativeExpr.bind("negative-length")),
137+
// Detect the expression: string("lit", 0, 0x1234567);
138+
hasArgument(2, LargeLengthExpr.bind("large-length")),
139+
// Detect the expression: string("lit", 1, 5)
140+
allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")),
141+
hasArgument(
142+
1, ignoringParenImpCasts(integerLiteral().bind("pos"))),
143+
hasArgument(2, ignoringParenImpCasts(
144+
integerLiteral().bind("length"))))))
118145
.bind("constructor"),
119146
this);
120147

@@ -155,14 +182,27 @@ void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) {
155182
diag(Loc, "constructor creating an empty string");
156183
} else if (Result.Nodes.getNodeAs<Expr>("negative-length")) {
157184
diag(Loc, "negative value used as length parameter");
185+
} else if (Result.Nodes.getNodeAs<Expr>("negative-pos")) {
186+
diag(Loc, "negative value used as position of the "
187+
"first character parameter");
158188
} else if (Result.Nodes.getNodeAs<Expr>("large-length")) {
159189
if (WarnOnLargeLength)
160190
diag(Loc, "suspicious large length parameter");
161191
} else if (Result.Nodes.getNodeAs<Expr>("literal-with-length")) {
162192
const auto *Str = Result.Nodes.getNodeAs<StringLiteral>("str");
163-
const auto *Lit = Result.Nodes.getNodeAs<IntegerLiteral>("int");
164-
if (Lit->getValue().ugt(Str->getLength())) {
193+
const auto *Length = Result.Nodes.getNodeAs<IntegerLiteral>("length");
194+
if (Length->getValue().ugt(Str->getLength())) {
165195
diag(Loc, "length is bigger than string literal size");
196+
return;
197+
}
198+
if (const auto *Pos = Result.Nodes.getNodeAs<IntegerLiteral>("pos")) {
199+
if (Pos->getValue().uge(Str->getLength())) {
200+
diag(Loc, "position of the first character parameter is bigger than "
201+
"string literal character range");
202+
} else if (Length->getValue().ugt(
203+
(Str->getLength() - Pos->getValue()).getZExtValue())) {
204+
diag(Loc, "length is bigger than remaining string literal size");
205+
}
166206
}
167207
} else if (const auto *Ptr = Result.Nodes.getNodeAs<Expr>("from-ptr")) {
168208
Expr::EvalResult ConstPtr;

clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp

Lines changed: 121 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,8 @@ static bool areEquivalentExpr(const Expr *Left, const Expr *Right) {
139139
return cast<BinaryOperator>(Left)->getOpcode() ==
140140
cast<BinaryOperator>(Right)->getOpcode();
141141
case Stmt::UnaryExprOrTypeTraitExprClass:
142-
const auto *LeftUnaryExpr =
143-
cast<UnaryExprOrTypeTraitExpr>(Left);
144-
const auto *RightUnaryExpr =
145-
cast<UnaryExprOrTypeTraitExpr>(Right);
142+
const auto *LeftUnaryExpr = cast<UnaryExprOrTypeTraitExpr>(Left);
143+
const auto *RightUnaryExpr = cast<UnaryExprOrTypeTraitExpr>(Right);
146144
if (LeftUnaryExpr->isArgumentType() && RightUnaryExpr->isArgumentType())
147145
return LeftUnaryExpr->getKind() == RightUnaryExpr->getKind() &&
148146
LeftUnaryExpr->getArgumentType() ==
@@ -699,7 +697,8 @@ static bool retrieveRelationalIntegerConstantExpr(
699697

700698
Symbol = OverloadedOperatorExpr->getArg(IntegerConstantIsFirstArg ? 1 : 0);
701699
OperandExpr = OverloadedOperatorExpr;
702-
Opcode = BinaryOperator::getOverloadedOpcode(OverloadedOperatorExpr->getOperator());
700+
Opcode = BinaryOperator::getOverloadedOpcode(
701+
OverloadedOperatorExpr->getOperator());
703702

704703
if (!retrieveIntegerConstantExpr(Result, Id, Value, ConstExpr))
705704
return false;
@@ -728,7 +727,8 @@ static bool retrieveRelationalIntegerConstantExpr(
728727
}
729728

730729
// Checks for expressions like (X == 4) && (Y != 9)
731-
static bool areSidesBinaryConstExpressions(const BinaryOperator *&BinOp, const ASTContext *AstCtx) {
730+
static bool areSidesBinaryConstExpressions(const BinaryOperator *&BinOp,
731+
const ASTContext *AstCtx) {
732732
const auto *LhsBinOp = dyn_cast<BinaryOperator>(BinOp->getLHS());
733733
const auto *RhsBinOp = dyn_cast<BinaryOperator>(BinOp->getRHS());
734734

@@ -747,6 +747,28 @@ static bool areSidesBinaryConstExpressions(const BinaryOperator *&BinOp, const A
747747
return false;
748748
}
749749

750+
static bool areSidesBinaryConstExpressionsOrDefinesOrIntegerConstant(
751+
const BinaryOperator *&BinOp, const ASTContext *AstCtx) {
752+
if (areSidesBinaryConstExpressions(BinOp, AstCtx))
753+
return true;
754+
755+
const Expr *Lhs = BinOp->getLHS();
756+
const Expr *Rhs = BinOp->getRHS();
757+
758+
if (!Lhs || !Rhs)
759+
return false;
760+
761+
auto IsDefineExpr = [AstCtx](const Expr *E) {
762+
const SourceRange Lsr = E->getSourceRange();
763+
if (!Lsr.getBegin().isMacroID() || E->isValueDependent() ||
764+
!E->isIntegerConstantExpr(*AstCtx))
765+
return false;
766+
return true;
767+
};
768+
769+
return IsDefineExpr(Lhs) || IsDefineExpr(Rhs);
770+
}
771+
750772
// Retrieves integer constant subexpressions from binary operator expressions
751773
// that have two equivalent sides.
752774
// E.g.: from (X == 5) && (X == 5) retrieves 5 and 5.
@@ -785,7 +807,7 @@ static bool retrieveConstExprFromBothSides(const BinaryOperator *&BinOp,
785807
}
786808

787809
static bool isSameRawIdentifierToken(const Token &T1, const Token &T2,
788-
const SourceManager &SM) {
810+
const SourceManager &SM) {
789811
if (T1.getKind() != T2.getKind())
790812
return false;
791813
if (T1.isNot(tok::raw_identifier))
@@ -808,8 +830,8 @@ static bool areExprsFromDifferentMacros(const Expr *LhsExpr,
808830
const ASTContext *AstCtx) {
809831
if (!LhsExpr || !RhsExpr)
810832
return false;
811-
SourceRange Lsr = LhsExpr->getSourceRange();
812-
SourceRange Rsr = RhsExpr->getSourceRange();
833+
const SourceRange Lsr = LhsExpr->getSourceRange();
834+
const SourceRange Rsr = RhsExpr->getSourceRange();
813835
if (!Lsr.getBegin().isMacroID() || !Rsr.getBegin().isMacroID())
814836
return false;
815837

@@ -847,11 +869,83 @@ static bool areExprsMacroAndNonMacro(const Expr *&LhsExpr,
847869
if (!LhsExpr || !RhsExpr)
848870
return false;
849871

850-
SourceLocation LhsLoc = LhsExpr->getExprLoc();
851-
SourceLocation RhsLoc = RhsExpr->getExprLoc();
872+
const SourceLocation LhsLoc = LhsExpr->getExprLoc();
873+
const SourceLocation RhsLoc = RhsExpr->getExprLoc();
852874

853875
return LhsLoc.isMacroID() != RhsLoc.isMacroID();
854876
}
877+
878+
static bool areStringsSameIgnoreSpaces(const llvm::StringRef Left,
879+
const llvm::StringRef Right) {
880+
if (Left == Right)
881+
return true;
882+
883+
// Do running comparison ignoring spaces
884+
llvm::StringRef L = Left.trim();
885+
llvm::StringRef R = Right.trim();
886+
while (!L.empty() && !R.empty()) {
887+
L = L.ltrim();
888+
R = R.ltrim();
889+
if (L.empty() && R.empty())
890+
return true;
891+
// If symbol compared are different ==> strings are not the same
892+
if (L.front() != R.front())
893+
return false;
894+
L = L.drop_front();
895+
R = R.drop_front();
896+
}
897+
return L.empty() && R.empty();
898+
}
899+
900+
static bool areExprsSameMacroOrLiteral(const BinaryOperator *BinOp,
901+
const ASTContext *Context) {
902+
903+
if (!BinOp)
904+
return false;
905+
906+
const Expr *Lhs = BinOp->getLHS();
907+
const Expr *Rhs = BinOp->getRHS();
908+
const SourceManager &SM = Context->getSourceManager();
909+
910+
const SourceRange Lsr = Lhs->getSourceRange();
911+
const SourceRange Rsr = Rhs->getSourceRange();
912+
if (Lsr.getBegin().isMacroID()) {
913+
// Left is macro so right macro too
914+
if (Rsr.getBegin().isMacroID()) {
915+
// Both sides are macros so they are same macro or literal
916+
const llvm::StringRef L = Lexer::getSourceText(
917+
CharSourceRange::getTokenRange(Lsr), SM, Context->getLangOpts(), 0);
918+
const llvm::StringRef R = Lexer::getSourceText(
919+
CharSourceRange::getTokenRange(Rsr), SM, Context->getLangOpts(), 0);
920+
return areStringsSameIgnoreSpaces(L, R);
921+
}
922+
// Left is macro but right is not so they are not same macro or literal
923+
return false;
924+
}
925+
const auto *Lil = dyn_cast<IntegerLiteral>(Lhs);
926+
const auto *Ril = dyn_cast<IntegerLiteral>(Rhs);
927+
if (Lil && Ril)
928+
return Lil->getValue() == Ril->getValue();
929+
930+
const auto *LStrl = dyn_cast<StringLiteral>(Lhs);
931+
const auto *RStrl = dyn_cast<StringLiteral>(Rhs);
932+
if (Lil && Ril) {
933+
const llvm::StringRef L = Lexer::getSourceText(
934+
CharSourceRange::getTokenRange(LStrl->getBeginLoc()), SM,
935+
Context->getLangOpts(), 0);
936+
const llvm::StringRef R = Lexer::getSourceText(
937+
CharSourceRange::getTokenRange(RStrl->getBeginLoc()), SM,
938+
Context->getLangOpts(), 0);
939+
return L.compare(R) == 0;
940+
}
941+
942+
const auto *Lbl = dyn_cast<CXXBoolLiteralExpr>(Lhs);
943+
const auto *Rbl = dyn_cast<CXXBoolLiteralExpr>(Rhs);
944+
if (Lbl && Rbl)
945+
return Lbl->getValue() == Rbl->getValue();
946+
947+
return false;
948+
}
855949
} // namespace
856950

857951
void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
@@ -1089,7 +1183,6 @@ static bool exprEvaluatesToSymbolic(BinaryOperatorKind Opcode, APSInt Value) {
10891183
((Opcode == BO_And || Opcode == BO_AndAssign) && ~Value == 0);
10901184
}
10911185

1092-
10931186
void RedundantExpressionCheck::checkBitwiseExpr(
10941187
const MatchFinder::MatchResult &Result) {
10951188
if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>(
@@ -1134,8 +1227,8 @@ void RedundantExpressionCheck::checkBitwiseExpr(
11341227
ConstExpr))
11351228
return;
11361229

1137-
if((Value != 0 && ~Value != 0) || Sym->getExprLoc().isMacroID())
1138-
return;
1230+
if ((Value != 0 && ~Value != 0) || Sym->getExprLoc().isMacroID())
1231+
return;
11391232

11401233
SourceLocation Loc = IneffectiveOperator->getOperatorLoc();
11411234

@@ -1276,19 +1369,23 @@ void RedundantExpressionCheck::check(const MatchFinder::MatchResult &Result) {
12761369
return;
12771370
}
12781371

1279-
if (areSidesBinaryConstExpressions(BinOp, Result.Context)) {
1372+
if (areSidesBinaryConstExpressionsOrDefinesOrIntegerConstant(
1373+
BinOp, Result.Context)) {
12801374
const Expr *LhsConst = nullptr, *RhsConst = nullptr;
12811375
BinaryOperatorKind MainOpcode{}, SideOpcode{};
1282-
1283-
if (!retrieveConstExprFromBothSides(BinOp, MainOpcode, SideOpcode,
1284-
LhsConst, RhsConst, Result.Context))
1285-
return;
1286-
1287-
if (areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) ||
1288-
areExprsMacroAndNonMacro(LhsConst, RhsConst))
1289-
return;
1376+
if (areSidesBinaryConstExpressions(BinOp, Result.Context)) {
1377+
if (!retrieveConstExprFromBothSides(BinOp, MainOpcode, SideOpcode,
1378+
LhsConst, RhsConst, Result.Context))
1379+
return;
1380+
1381+
if (areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) ||
1382+
areExprsMacroAndNonMacro(LhsConst, RhsConst))
1383+
return;
1384+
} else {
1385+
if (!areExprsSameMacroOrLiteral(BinOp, Result.Context))
1386+
return;
1387+
}
12901388
}
1291-
12921389
diag(BinOp->getOperatorLoc(), "both sides of operator are equivalent");
12931390
}
12941391

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,19 @@ New check aliases
9797
Changes in existing checks
9898
^^^^^^^^^^^^^^^^^^^^^^^^^^
9999

100+
- Improved :doc:`bugprone-string-constructor
101+
<clang-tidy/checks/bugprone/string-constructor>` check to find suspicious
102+
calls of ``std::string`` constructor with char pointer, start position and
103+
length parameters.
104+
100105
- Improved :doc:`bugprone-unsafe-functions
101106
<clang-tidy/checks/bugprone/unsafe-functions>` check to allow specifying
102107
additional C++ member functions to match.
103108

109+
- Improved :doc:`misc-redundant-expression
110+
<clang-tidy/checks/misc/redundant-expression>` check by providing additional
111+
examples and fixing some macro related false positives.
112+
104113
Removed checks
105114
^^^^^^^^^^^^^^
106115

clang-tools-extra/docs/clang-tidy/checks/bugprone/string-constructor.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Examples:
2121
.. code-block:: c++
2222

2323
std::string("test", 200); // Will include random characters after "test".
24+
std::string("test", 2, 5); // Will include random characters after "st".
2425
std::string_view("test", 200);
2526

2627
Creating an empty string from constructors with parameters is considered
@@ -31,8 +32,19 @@ Examples:
3132
.. code-block:: c++
3233

3334
std::string("test", 0); // Creation of an empty string.
35+
std::string("test", 1, 0);
3436
std::string_view("test", 0);
3537

38+
Passing an invalid first character position parameter to constructor will
39+
cause ``std::out_of_range`` exception at runtime.
40+
41+
Examples:
42+
43+
.. code-block:: c++
44+
45+
std::string("test", -1, 10); // Negative first character position.
46+
std::string("test", 10, 10); // First character position is bigger than string literal character range".
47+
3648
Options
3749
-------
3850

0 commit comments

Comments
 (0)