Skip to content

Commit cda2378

Browse files
SC llvm teamSC llvm team
authored andcommitted
Merged main:28ae42e66251 into amd-gfx:b6fe60a67eed
Local branch amd-gfx b6fe60a Merged main:1d4601a1ef84 into amd-gfx:13ef5174ada6 Remote branch main 28ae42e [GISel] Add LookThroughInstrs for getIConstantVRegVal and getIConstan… (llvm#68327)
2 parents b6fe60a + 28ae42e commit cda2378

File tree

28 files changed

+1810
-37
lines changed

28 files changed

+1810
-37
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
#include "clang/Analysis/Analyses/UnsafeBufferUsage.h"
1010
#include "clang/AST/Decl.h"
11+
#include "clang/AST/Expr.h"
1112
#include "clang/AST/RecursiveASTVisitor.h"
13+
#include "clang/AST/StmtVisitor.h"
1214
#include "clang/ASTMatchers/ASTMatchFinder.h"
1315
#include "clang/Lex/Lexer.h"
1416
#include "clang/Lex/Preprocessor.h"
@@ -22,6 +24,52 @@ using namespace llvm;
2224
using namespace clang;
2325
using namespace ast_matchers;
2426

27+
#ifndef NDEBUG
28+
namespace {
29+
class StmtDebugPrinter
30+
: public ConstStmtVisitor<StmtDebugPrinter, std::string> {
31+
public:
32+
std::string VisitStmt(const Stmt *S) { return S->getStmtClassName(); }
33+
34+
std::string VisitBinaryOperator(const BinaryOperator *BO) {
35+
return "BinaryOperator(" + BO->getOpcodeStr().str() + ")";
36+
}
37+
38+
std::string VisitUnaryOperator(const UnaryOperator *UO) {
39+
return "UnaryOperator(" + UO->getOpcodeStr(UO->getOpcode()).str() + ")";
40+
}
41+
42+
std::string VisitImplicitCastExpr(const ImplicitCastExpr *ICE) {
43+
return "ImplicitCastExpr(" + std::string(ICE->getCastKindName()) + ")";
44+
}
45+
};
46+
47+
// Returns a string of ancestor `Stmt`s of the given `DRE` in such a form:
48+
// "DRE ==> parent-of-DRE ==> grandparent-of-DRE ==> ...".
49+
static std::string getDREAncestorString(const DeclRefExpr *DRE,
50+
ASTContext &Ctx) {
51+
std::stringstream SS;
52+
const Stmt *St = DRE;
53+
StmtDebugPrinter StmtPriner;
54+
55+
do {
56+
SS << StmtPriner.Visit(St);
57+
58+
DynTypedNodeList StParents = Ctx.getParents(*St);
59+
60+
if (StParents.size() > 1)
61+
return "unavailable due to multiple parents";
62+
if (StParents.size() == 0)
63+
break;
64+
St = StParents.begin()->get<Stmt>();
65+
if (St)
66+
SS << " ==> ";
67+
} while (St);
68+
return SS.str();
69+
}
70+
} // namespace
71+
#endif /* NDEBUG */
72+
2573
namespace clang::ast_matchers {
2674
// A `RecursiveASTVisitor` that traverses all descendants of a given node "n"
2775
// except for those belonging to a different callable of "n".
@@ -2589,11 +2637,15 @@ void clang::checkUnsafeBufferUsage(const Decl *D,
25892637
#ifndef NDEBUG
25902638
auto AllUnclaimed = Tracker.getUnclaimedUses(it->first);
25912639
for (auto UnclaimedDRE : AllUnclaimed) {
2592-
Handler.addDebugNoteForVar(
2593-
it->first, UnclaimedDRE->getBeginLoc(),
2594-
("failed to produce fixit for '" + it->first->getNameAsString() +
2595-
"' : has an unclaimed use"));
2596-
}
2640+
std::string UnclaimedUseTrace =
2641+
getDREAncestorString(UnclaimedDRE, D->getASTContext());
2642+
2643+
Handler.addDebugNoteForVar(
2644+
it->first, UnclaimedDRE->getBeginLoc(),
2645+
("failed to produce fixit for '" + it->first->getNameAsString() +
2646+
"' : has an unclaimed use\nThe unclaimed DRE trace: " +
2647+
UnclaimedUseTrace));
2648+
}
25972649
#endif
25982650
it = FixablesForAllVars.byVar.erase(it);
25992651
} else if (it->first->isInitCapture()) {

clang/lib/Lex/ModuleMap.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,16 +1398,17 @@ bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
13981398
}
13991399

14001400
bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
1401-
auto Unresolved = std::move(Mod->UnresolvedDirectUses);
1402-
Mod->UnresolvedDirectUses.clear();
1401+
auto *Top = Mod->getTopLevelModule();
1402+
auto Unresolved = std::move(Top->UnresolvedDirectUses);
1403+
Top->UnresolvedDirectUses.clear();
14031404
for (auto &UDU : Unresolved) {
1404-
Module *DirectUse = resolveModuleId(UDU, Mod, Complain);
1405+
Module *DirectUse = resolveModuleId(UDU, Top, Complain);
14051406
if (DirectUse)
1406-
Mod->DirectUses.push_back(DirectUse);
1407+
Top->DirectUses.push_back(DirectUse);
14071408
else
1408-
Mod->UnresolvedDirectUses.push_back(UDU);
1409+
Top->UnresolvedDirectUses.push_back(UDU);
14091410
}
1410-
return !Mod->UnresolvedDirectUses.empty();
1411+
return !Top->UnresolvedDirectUses.empty();
14111412
}
14121413

14131414
bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: %clang_cc1 -fmodules-cache-path=%t -fmodules -fimplicit-module-maps -I %t %t/no-undeclared-includes.c -verify
4+
5+
//--- no-undeclared-includes.c
6+
// expected-no-diagnostics
7+
#include <assert.h>
8+
9+
//--- assert.h
10+
#include <base.h>
11+
12+
//--- base.h
13+
#ifndef base_h
14+
#define base_h
15+
16+
17+
18+
#endif /* base_h */
19+
20+
//--- module.modulemap
21+
module cstd [system] [no_undeclared_includes] {
22+
use base
23+
module assert {
24+
textual header "assert.h"
25+
}
26+
}
27+
28+
module base [system] {
29+
header "base.h"
30+
export *
31+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- Python -*-
2+
3+
config.substitutions.append(
4+
(
5+
"%analyze_safe_buffer_debug_notes",
6+
"'%s' %s" % (
7+
config.python_executable,
8+
os.path.join(config.clang_src_dir, "utils", "analyze_safe_buffer_debug_notes.py")
9+
)
10+
)
11+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %clang_cc1 -Wno-unused-value -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
2+
// RUN: -mllvm -debug-only=SafeBuffers \
3+
// RUN: -std=c++20 -verify=expected %s
4+
5+
// RUN: %clang_cc1 -Wno-unused-value -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
6+
// RUN: -mllvm -debug-only=SafeBuffers \
7+
// RUN: -std=c++20 %s \
8+
// RUN: 2>&1 | grep 'The unclaimed DRE trace:' \
9+
// RUN: | sed 's/^The unclaimed DRE trace://' \
10+
// RUN: | %analyze_safe_buffer_debug_notes \
11+
// RUN: | FileCheck %s
12+
13+
// This debugging facility is only available in debug builds.
14+
//
15+
// REQUIRES: asserts
16+
// REQUIRES: shell
17+
18+
void test_unclaimed_use(int *p) { // expected-warning{{'p' is an unsafe pointer used for buffer access}}
19+
p++; // expected-note{{used in pointer arithmetic here}} \
20+
expected-note{{safe buffers debug: failed to produce fixit for 'p' : has an unclaimed use\n \
21+
The unclaimed DRE trace: DeclRefExpr, UnaryOperator(++), CompoundStmt}}
22+
*((p + 1) + 1); // expected-warning{{unsafe pointer arithmetic}} \
23+
expected-note{{used in pointer arithmetic here}} \
24+
expected-note{{safe buffers debug: failed to produce fixit for 'p' : has an unclaimed use\n \
25+
The unclaimed DRE trace: DeclRefExpr, ImplicitCastExpr(LValueToRValue), BinaryOperator(+), ParenExpr, BinaryOperator(+), ParenExpr, UnaryOperator(*), CompoundStmt}}
26+
p -= 1; // expected-note{{used in pointer arithmetic here}} \
27+
expected-note{{safe buffers debug: failed to produce fixit for 'p' : has an unclaimed use\n \
28+
The unclaimed DRE trace: DeclRefExpr, BinaryOperator(-=), CompoundStmt}}
29+
p--; // expected-note{{used in pointer arithmetic here}} \
30+
expected-note{{safe buffers debug: failed to produce fixit for 'p' : has an unclaimed use\n \
31+
The unclaimed DRE trace: DeclRefExpr, UnaryOperator(--), CompoundStmt}}
32+
p[5] = 5; // expected-note{{used in buffer access here}}
33+
}
34+
35+
// CHECK: Root # 1
36+
// CHECK: |- DeclRefExpr # 4
37+
// CHECK: |-- UnaryOperator(++) # 1
38+
// CHECK: |--- CompoundStmt # 1
39+
// CHECK: |-- ImplicitCastExpr(LValueToRValue) # 1
40+
// CHECK: |--- BinaryOperator(+) # 1
41+
// CHECK: |---- ParenExpr # 1
42+
// CHECK: |----- BinaryOperator(+) # 1
43+
// CHECK: |------ ParenExpr # 1
44+
// CHECK: |------- UnaryOperator(*) # 1
45+
// CHECK: |-------- CompoundStmt # 1
46+
// CHECK: |-- BinaryOperator(-=) # 1
47+
// CHECK: |--- CompoundStmt # 1
48+
// CHECK: |-- UnaryOperator(--) # 1
49+
// CHECK: |--- CompoundStmt # 1

clang/unittests/Driver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(LLVM_LINK_COMPONENTS
99
add_clang_unittest(ClangDriverTests
1010
DistroTest.cpp
1111
DXCModeTest.cpp
12+
GCCVersionTest.cpp
1213
ToolChainTest.cpp
1314
ModuleCacheTest.cpp
1415
MultilibBuilderTest.cpp
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===- unittests/Driver/GCCVersionTest.cpp --- GCCVersion parser tests ----===//
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+
// Unit tests for Generic_GCC::GCCVersion
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "../../lib/Driver/ToolChains/Gnu.h"
14+
#include "gtest/gtest.h"
15+
16+
// The Generic_GCC class is hidden in dylib/shared library builds, so
17+
// this test can only be built if neither of those configurations are
18+
// enabled.
19+
#if !defined(LLVM_BUILD_LLVM_DYLIB) && !defined(LLVM_BUILD_SHARED_LIBS)
20+
21+
using namespace clang;
22+
using namespace clang::driver;
23+
24+
namespace {
25+
26+
struct VersionParseTest {
27+
std::string Text;
28+
29+
int Major, Minor, Patch;
30+
std::string MajorStr, MinorStr, PatchSuffix;
31+
};
32+
33+
const VersionParseTest TestCases[] = {
34+
{"5", 5, -1, -1, "5", "", ""},
35+
{"4.4", 4, 4, -1, "4", "4", ""},
36+
{"4.4-patched", 4, 4, -1, "4", "4", "-patched"},
37+
{"4.4.0", 4, 4, 0, "4", "4", ""},
38+
{"4.4.x", 4, 4, -1, "4", "4", ""},
39+
{"4.4.2-rc4", 4, 4, 2, "4", "4", "-rc4"},
40+
{"4.4.x-patched", 4, 4, -1, "4", "4", ""},
41+
{"not-a-version", -1, -1, -1, "", "", ""},
42+
};
43+
44+
TEST(GCCVersionTest, Parse) {
45+
for (const auto &TC : TestCases) {
46+
auto V = toolchains::Generic_GCC::GCCVersion::Parse(TC.Text);
47+
EXPECT_EQ(V.Text, TC.Text);
48+
EXPECT_EQ(V.Major, TC.Major);
49+
EXPECT_EQ(V.Minor, TC.Minor);
50+
EXPECT_EQ(V.Patch, TC.Patch);
51+
EXPECT_EQ(V.MajorStr, TC.MajorStr);
52+
EXPECT_EQ(V.MinorStr, TC.MinorStr);
53+
EXPECT_EQ(V.PatchSuffix, TC.PatchSuffix);
54+
}
55+
}
56+
57+
} // end anonymous namespace
58+
59+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
from collections import OrderedDict
3+
4+
class Trie:
5+
def __init__(self, name):
6+
self.name = name
7+
self.children = OrderedDict()
8+
self.count = 1
9+
10+
def add(self, name):
11+
if name in self.children:
12+
self.children[name].count += 1
13+
else:
14+
self.children[name] = Trie(name)
15+
return self.children[name]
16+
17+
def print(self, depth):
18+
if depth > 0:
19+
print('|', end="")
20+
for i in range(depth):
21+
print('-', end="")
22+
if depth > 0:
23+
print(end=" ")
24+
print(self.name, '#', self.count)
25+
for key, child in self.children.items():
26+
child.print(depth + 1)
27+
28+
29+
Root = Trie("Root")
30+
31+
if __name__ == "__main__":
32+
for line in sys.stdin:
33+
words = line.split('==>')
34+
words = [word.strip() for word in words]
35+
MyTrie = Root;
36+
for word in words:
37+
MyTrie = MyTrie.add(word)
38+
39+
Root.print(0)

compiler-rt/lib/builtins/int_to_fp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ static __inline int clzSrcT(usrc_t x) { return __clzti2(x); }
4444
typedef float dst_t;
4545
typedef uint32_t dst_rep_t;
4646
#define DST_REP_C UINT32_C
47-
static const int dstSigBits = 23;
47+
48+
enum {
49+
dstSigBits = 23,
50+
};
4851

4952
#elif defined DST_DOUBLE
5053
typedef double dst_t;

compiler-rt/lib/builtins/int_to_fp_impl.inc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
static __inline dst_t __floatXiYf__(src_t a) {
1818
if (a == 0)
1919
return 0.0;
20-
const int dstMantDig = dstSigBits + 1;
21-
const int srcBits = sizeof(src_t) * CHAR_BIT;
22-
const int srcIsSigned = ((src_t)-1) < 0;
20+
21+
enum {
22+
dstMantDig = dstSigBits + 1,
23+
srcBits = sizeof(src_t) * CHAR_BIT,
24+
srcIsSigned = ((src_t)-1) < 0,
25+
};
26+
2327
const src_t s = srcIsSigned ? a >> (srcBits - 1) : 0;
28+
2429
a = (usrc_t)(a ^ s) - s;
2530
int sd = srcBits - clzSrcT(a); // number of significant digits
2631
int e = sd - 1; // exponent

lld/COFF/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ struct Configuration {
316316
bool stdcallFixup = false;
317317
bool writeCheckSum = false;
318318
EmitKind emit = EmitKind::Obj;
319+
bool allowDuplicateWeak = false;
319320
};
320321

321322
} // namespace lld::coff

lld/COFF/Driver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
20282028
config->stdcallFixup =
20292029
args.hasFlag(OPT_stdcall_fixup, OPT_stdcall_fixup_no, config->mingw);
20302030
config->warnStdcallFixup = !args.hasArg(OPT_stdcall_fixup);
2031+
config->allowDuplicateWeak =
2032+
args.hasFlag(OPT_lld_allow_duplicate_weak,
2033+
OPT_lld_allow_duplicate_weak_no, config->mingw);
20312034

20322035
if (args.hasFlag(OPT_inferasanlibs, OPT_inferasanlibs_no, false))
20332036
warn("ignoring '/inferasanlibs', this flag is not supported");

lld/COFF/InputFiles.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void checkAndSetWeakAlias(COFFLinkerContext &ctx, InputFile *f,
8181
// of another symbol emitted near the weak symbol.
8282
// Just use the definition from the first object file that defined
8383
// this weak symbol.
84-
if (ctx.config.mingw)
84+
if (ctx.config.allowDuplicateWeak)
8585
return;
8686
ctx.symtab.reportDuplicate(source, f);
8787
}

lld/COFF/Options.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ defm demangle : B<"demangle",
232232
def include_optional : Joined<["/", "-", "/?", "-?"], "includeoptional:">,
233233
HelpText<"Add symbol as undefined, but allow it to remain undefined">;
234234
def kill_at : F<"kill-at">;
235+
defm lld_allow_duplicate_weak : B_priv<"lld-allow-duplicate-weak">;
235236
def lldemit : P<"lldemit", "Specify output type">;
236237
def lldmingw : F<"lldmingw">;
237238
def noseh : F<"noseh">;

lld/test/COFF/gnu-weak.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
RUN: lld-link -lldmingw %S/Inputs/gnu-weak.o %S/Inputs/gnu-weak2.o -out:%t.exe
2+
RUN: lld-link -lld-allow-duplicate-weak %S/Inputs/gnu-weak.o %S/Inputs/gnu-weak2.o -out:%t.exe
3+
RUN: not lld-link %S/Inputs/gnu-weak.o %S/Inputs/gnu-weak2.o -out:%t.exe 2>&1 | FileCheck %s --check-prefix=DEFAULT-ERROR
4+
5+
DEFAULT-ERROR: error: duplicate symbol: weakfunc
6+
27

38
GNU ld can handle several definitions of the same weak symbol, and
49
unless there is a strong definition of it, it just picks the first

0 commit comments

Comments
 (0)