Skip to content

Commit 9a50452

Browse files
committed
Remove StringRef deprecation warning
1 parent 8ea2e79 commit 9a50452

File tree

5 files changed

+45
-35
lines changed

5 files changed

+45
-35
lines changed

lib/passes/analysis/MemInstFinder.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,9 @@ bool MemInstFinderPass::runOnModule(Module& module) {
191191
return true;
192192
}
193193

194-
if (name.startswith("llvm.") || name.startswith("__llvm_gcov") ||
195-
name.startswith("__llvm_gcda") || name.startswith("__profn")) {
196-
// 2nd and 3rd check: Check if the global is private gcov data (--coverage).
197-
LOG_DEBUG("LLVM startswith \"llvm\"")
198-
return true;
199-
}
200-
201-
if (name.startswith("___asan") || name.startswith("__msan") || name.startswith("__tsan")) {
202-
LOG_DEBUG("LLVM startswith \"sanitizer\"")
194+
if (util::starts_with_any_of(name, "llvm.", "__llvm_gcov", "__llvm_gcda", "__profn", "___asan",
195+
"__msan", "__tsan")) {
196+
LOG_DEBUG("Prefixed matched on " << name)
203197
return true;
204198
}
205199

@@ -267,7 +261,7 @@ bool MemInstFinderPass::runOnModule(Module& module) {
267261
} // namespace typeart
268262

269263
bool MemInstFinderPass::runOnFunction(llvm::Function& function) {
270-
if (function.isDeclaration() || function.getName().startswith("__typeart")) {
264+
if (function.isDeclaration() || util::starts_with_any_of(function.getName(), "__typeart")) {
271265
return false;
272266
}
273267

lib/passes/filter/Matcher.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ class NoMatcher final : public Matcher {
4343
};
4444

4545
class DefaultStringMatcher final : public Matcher {
46-
Regex matcher;
46+
llvm::Regex matcher;
4747

4848
public:
49-
explicit DefaultStringMatcher(const std::string& regex) : matcher(regex, Regex::NoFlags) {
49+
explicit DefaultStringMatcher(const std::string& regex) : matcher(regex, llvm::Regex::NoFlags) {
5050
}
5151

5252
MatchResult match(llvm::CallSite c) const override {
@@ -82,13 +82,13 @@ class FunctionOracleMatcher final : public Matcher {
8282
if (skip_set.count(f_name) > 0) {
8383
return MatchResult::ShouldSkip;
8484
}
85-
if (f_name_ref.startswith("__typeart_")) {
85+
if (util::starts_with_any_of(f_name_ref, "__typeart_")) {
8686
return MatchResult::ShouldSkip;
8787
}
8888
if (mem_operations.kind(f_name)) {
8989
return MatchResult::ShouldSkip;
9090
}
91-
if (f_name_ref.startswith("__ubsan") || f_name_ref.startswith("__asan") || f_name_ref.startswith("__msan")) {
91+
if (util::starts_with_any_of(f_name_ref, "__ubsan", "__asan", "__msan")) {
9292
return MatchResult::ShouldContinue;
9393
}
9494
}

lib/passes/filter/OmpUtil.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "compat/CallSite.h"
1818
#include "support/DefUseChain.h"
1919
#include "support/OmpUtil.h"
20+
#include "support/Util.h"
2021

2122
#include "llvm/IR/Function.h"
2223
#include "llvm/IR/Instructions.h"
@@ -37,31 +38,31 @@ struct OmpContext {
3738
const auto called = c.getCalledFunction();
3839
if (called != nullptr) {
3940
// TODO probably not complete (openmp task?, see isOmpTask*())
40-
return called->getName().startswith("__kmpc_fork_call");
41+
return util::starts_with_any_of(called->getName(), "__kmpc_fork_call");
4142
}
4243
return false;
4344
}
4445

4546
static bool isOmpTaskAlloc(const llvm::CallSite& c) {
4647
const auto called = c.getCalledFunction();
4748
if (called != nullptr) {
48-
return called->getName().startswith("__kmpc_omp_task_alloc");
49+
return util::starts_with_any_of(called->getName(), "__kmpc_omp_task_alloc");
4950
}
5051
return false;
5152
}
5253

5354
static bool isOmpTaskCall(const llvm::CallSite& c) {
5455
const auto called = c.getCalledFunction();
5556
if (called != nullptr) {
56-
return called->getName().endswith("__kmpc_omp_task");
57+
return util::ends_with_any_of(called->getName(), "__kmpc_omp_task");
5758
}
5859
return false;
5960
}
6061

6162
static bool isOmpTaskRelated(const llvm::CallSite& c) {
6263
const auto called = c.getCalledFunction();
6364
if (called != nullptr) {
64-
return called->getName().startswith("__kmpc_omp_task");
65+
return util::starts_with_any_of(called->getName(), "__kmpc_omp_task");
6566
}
6667
return false;
6768
}
@@ -71,9 +72,8 @@ struct OmpContext {
7172
if (!is_execute) {
7273
const auto called = c.getCalledFunction();
7374
if (called != nullptr) {
74-
const auto name = called->getName();
7575
// TODO extend this if required
76-
return name.startswith("__kmpc") || name.startswith("omp_");
76+
return util::starts_with_any_of(called->getName(), "__kmpc", "omp_");
7777
}
7878
}
7979
return false;
@@ -142,7 +142,8 @@ struct OmpContext {
142142
llvm::CallSite site(value);
143143
if (site.isCall() || site.isInvoke()) {
144144
const auto called = site.getCalledFunction();
145-
if (called != nullptr && called->getName().startswith("__kmpc_omp_task(")) {
145+
146+
if (called != nullptr && util::starts_with_any_of(called->getName(), "__kmpc_omp_task(")) {
146147
found = true;
147148
return util::DefUseChain::cancel;
148149
}
@@ -171,7 +172,7 @@ struct OmpContext {
171172
if (s.isCall() || s.isInvoke()) {
172173
if (auto f = s.getCalledFunction()) {
173174
// once true, the find_all should cancel
174-
return f->getName().startswith("__kmpc_omp_task_alloc");
175+
return util::starts_with_any_of(f->getName(), "__kmpc_omp_task_alloc");
175176
}
176177
}
177178
return false;

lib/passes/support/OmpUtil.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ inline bool isOmpContext(llvm::Function* f) {
2424
if (f != nullptr) {
2525
const auto name_ = demangle(f->getName());
2626
llvm::StringRef fname(name_);
27-
return fname.startswith(".omp") || fname.startswith("__kmpc") || fname.startswith("__omp");
27+
return util::starts_with_any_of(fname, ".omp", "__kmpc", "__omp");
2828
}
2929
return false;
3030
}

lib/passes/support/Util.h

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include "llvm/Support/Regex.h"
2323
#include "llvm/Support/raw_ostream.h"
2424

25+
#include <algorithm>
26+
#include <llvm/ADT/StringRef.h>
27+
#include <string>
28+
2529
namespace typeart::util {
2630

2731
namespace detail {
@@ -126,18 +130,6 @@ inline std::vector<llvm::Instruction*> find_all(llvm::Function* f, Predicate&& p
126130
return v;
127131
}
128132

129-
template <typename Predicate>
130-
inline llvm::Instruction* find_first_of(llvm::Function* f, Predicate&& p) {
131-
for (auto& bb : *f) {
132-
for (auto& inst : bb) {
133-
if (p(inst)) {
134-
return &inst;
135-
}
136-
}
137-
}
138-
return nullptr;
139-
}
140-
141133
inline bool regex_matches(const std::string& regex, const std::string& in, bool case_sensitive = false) {
142134
using namespace llvm;
143135
Regex r(regex, !case_sensitive ? Regex::IgnoreCase : Regex::NoFlags);
@@ -187,6 +179,29 @@ inline std::string glob2regex(const std::string& glob) {
187179
return glob_reg;
188180
}
189181

182+
template <typename... StringTy>
183+
bool with_any_of(llvm::StringRef lhs, StringTy&&... rhs) {
184+
return !lhs.empty() && ((lhs == rhs) || ...);
185+
}
186+
187+
template <typename... StringTy>
188+
inline bool starts_with_any_of(llvm::StringRef lhs, StringTy... rhs) {
189+
#if LLVM_VERSION_MAJOR > 14
190+
return !lhs.empty() && ((lhs.starts_with(rhs)) || ...);
191+
#else
192+
return !lhs.empty() && ((lhs.startswith(rhs)) || ...);
193+
#endif
194+
}
195+
196+
template <typename... StringTy>
197+
inline bool ends_with_any_of(llvm::StringRef lhs, StringTy... rhs) {
198+
#if LLVM_VERSION_MAJOR > 14
199+
return !lhs.empty() && ((lhs.ends_with(rhs)) || ...);
200+
#else
201+
return !lhs.empty() && ((lhs.endswith(rhs)) || ...);
202+
#endif
203+
}
204+
190205
} // namespace typeart::util
191206

192207
#endif /* LIB_UTIL_H_ */

0 commit comments

Comments
 (0)