Skip to content

Commit 93123b1

Browse files
[SYCLomatic][NFC] Refactor utility (#2486)
Also removed dead code.
1 parent a3da58f commit 93123b1

File tree

24 files changed

+507
-562
lines changed

24 files changed

+507
-562
lines changed

clang/include/clang/DPCT/DPCTOptions.inc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787

8888
#ifndef DPCT_ENUM_OPTION
8989
#define DPCT_ENUM_OPTION(TEMPLATE, ENUM_TYPE, ...) \
90-
DPCT_OPTION(TEMPLATE, ENUM_TYPE, __VA_ARGS__)
90+
DPCT_OPTION(TEMPLATE, clang::dpct::ENUM_TYPE, __VA_ARGS__)
9191
#endif
9292

9393
#ifndef DPCT_FLAG_OPTION
@@ -478,7 +478,7 @@ DPCT_ENUM_OPTION(
478478
llvm::cl::Optional)
479479

480480
DPCT_ENUM_OPTION(
481-
clang::dpct::opt, format::FormatRange, FormatRng,
481+
clang::dpct::opt, FormatRange, FormatRng,
482482
clang::dpct::DpctOptionClass::OC_Attribute,
483483
DPCT_OPTION_ACTIONS(clang::dpct::DpctActionKind::DAK_Migration),
484484
"format-range",
@@ -599,25 +599,25 @@ DPCT_ENUM_OPTION(
599599
clang::dpct::DpctActionKind::DAK_Analysis),
600600
"use-explicit-namespace",
601601
DPCT_OPTION_VALUES(
602-
DPCT_OPTION_ENUM_VALUE("dpct", int(ExplicitNamespace::EN_DPCT),
602+
DPCT_OPTION_ENUM_VALUE("dpct", int(clang::dpct::ExplicitNamespace::EN_DPCT),
603603
"Generate code with dpct:: namespace.", false),
604-
DPCT_OPTION_ENUM_VALUE("none", int(ExplicitNamespace::EN_None),
604+
DPCT_OPTION_ENUM_VALUE("none", int(clang::dpct::ExplicitNamespace::EN_None),
605605
"Generate code without any namespaces. Cannot "
606606
"be used with other values.",
607607
false),
608608
DPCT_OPTION_ENUM_VALUE(
609-
"sycl", int(ExplicitNamespace::EN_SYCL),
609+
"sycl", int(clang::dpct::ExplicitNamespace::EN_SYCL),
610610
"Generate code with sycl:: namespace. Cannot be used with cl or "
611611
"sycl-math values.",
612612
false),
613613
DPCT_OPTION_ENUM_VALUE(
614-
"sycl-math", int(ExplicitNamespace::EN_SYCL_Math),
614+
"sycl-math", int(clang::dpct::ExplicitNamespace::EN_SYCL_Math),
615615
"Generate code with sycl:: namespace, applied only for "
616616
"SYCL math functions.\n"
617617
"Cannot be used with cl or sycl values.",
618618
false),
619619
DPCT_OPTION_ENUM_VALUE("syclcompat",
620-
int(ExplicitNamespace::EN_SYCLCompat),
620+
int(clang::dpct::ExplicitNamespace::EN_SYCLCompat),
621621
"Generate code with syclcompat:: namespace.",
622622
false)),
623623
llvm::cl::desc("Define the namespaces to use explicitly in generated "

clang/lib/DPCT/AnalysisInfo.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
#define TYPELOC_CAST(Target) static_cast<const Target &>(TL)
2828

2929
llvm::StringRef getReplacedName(const clang::NamedDecl *D) {
30-
auto Iter = MapNames::TypeNamesMap.find(D->getQualifiedNameAsString(false));
31-
if (Iter != MapNames::TypeNamesMap.end()) {
32-
auto Range = getDefinitionRange(D->getBeginLoc(), D->getEndLoc());
30+
auto Iter = clang::dpct::MapNames::TypeNamesMap.find(D->getQualifiedNameAsString(false));
31+
if (Iter != clang::dpct::MapNames::TypeNamesMap.end()) {
32+
auto Range = clang::dpct::getDefinitionRange(D->getBeginLoc(), D->getEndLoc());
3333
for (auto ItHeader = Iter->second->Includes.begin();
3434
ItHeader != Iter->second->Includes.end(); ItHeader++) {
3535
clang::dpct::DpctGlobalInfo::getInstance().insertHeader(Range.getBegin(),
@@ -6987,7 +6987,7 @@ void CallFunctionExpr::buildInfo() {
69876987
bool isInSameLine(SourceLocation First, SourceLocation Second,
69886988
const SourceManager &SM) {
69896989
bool Invalid = false;
6990-
return ::isInSameLine(SM.getExpansionLoc(First), SM.getExpansionLoc(Second),
6990+
return isInSameLine(SM.getExpansionLoc(First), SM.getExpansionLoc(Second),
69916991
SM, Invalid) &&
69926992
!Invalid;
69936993
}

clang/lib/DPCT/AnalysisInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void setGetReplacedNamePtr(llvm::StringRef (*Ptr)(const clang::NamedDecl *D));
4343

4444
namespace clang {
4545
namespace dpct {
46+
47+
using format::FormatRange;
4648
using LocInfo = std::pair<tooling::UnifiedPath, unsigned int>;
4749
template <class F, class... Ts>
4850
std::string buildStringFromPrinter(F Func, Ts &&...Args) {

clang/lib/DPCT/CommandOption/ValidateArguments.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ using namespace llvm;
2323
using namespace std;
2424
namespace path = llvm::sys::path;
2525
namespace fs = llvm::sys::fs;
26+
27+
namespace clang {
28+
namespace dpct {
29+
2630
bool isOutRootAccess(SmallString<256> &OutRoot) {
2731
if (!fs::can_write(OutRoot)) {
2832
llvm::errs() << "Could not access out-root directory.\n";
@@ -95,7 +99,7 @@ bool makeInRootCanonicalOrSetDefaults(
9599
return false;
96100
} else if (InRoot.getCanonicalPath().empty()) {
97101
clang::dpct::ShowStatus(MigrationErrorInvalidInRootPath);
98-
dpctExit(MigrationErrorInvalidInRootPath);
102+
clang::dpct::dpctExit(MigrationErrorInvalidInRootPath);
99103
}
100104
if (fs::get_file_type(InRoot.getCanonicalPath()) !=
101105
fs::file_type::directory_file) {
@@ -168,7 +172,7 @@ int validateBuildScriptPaths(const clang::tooling::UnifiedPath &InRoot,
168172
<< InRoot << "'\n";
169173
}
170174
if (fs::is_regular_file(Canonical.getCanonicalPath()) &&
171-
!isChildPath(InRoot, Canonical)) {
175+
!clang::dpct::isChildPath(InRoot, Canonical)) {
172176
Ok = -4;
173177
llvm::errs() << "Error: File '" << Canonical.getCanonicalPath()
174178
<< "' is not under the specified input root directory '"
@@ -243,3 +247,5 @@ bool checkReportArgs(ReportTypeEnum &RType, ReportFormatEnum &RFormat,
243247

244248
return Success;
245249
}
250+
} // namespace dpct
251+
} // namespace clang

clang/lib/DPCT/CommandOption/ValidateArguments.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#include <string>
1515
#include <vector>
1616

17+
namespace clang {
18+
namespace dpct {
19+
1720
#if defined(_WIN32)
1821
#define MAX_PATH_LEN _MAX_PATH
1922
#define MAX_NAME_LEN _MAX_FNAME
@@ -140,4 +143,6 @@ bool checkReportArgs(ReportTypeEnum &RType, ReportFormatEnum &RFormat,
140143
/// -1: Path is invalid
141144
int checkSDKPathOrIncludePath(clang::tooling::UnifiedPath &Path);
142145

146+
} // namespace dpct
147+
} // namespace clang
143148
#endif // DPCT_VALIDATE_ARGUMENTS_H

clang/lib/DPCT/DPCT.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ static llvm::cl::opt<AutoCompletePrinter, true, llvm::cl::parser<std::string>> A
139139
// TODO: implement one of this for each source language.
140140
UnifiedPath CudaPath;
141141
UnifiedPath DpctInstallPath;
142+
142143
std::unordered_map<std::string, bool> ChildOrSameCache;
143144
std::unordered_map<std::string, bool> ChildPathCache;
144145
std::unordered_map<std::string, bool> IsDirectoryCache;
@@ -1066,7 +1067,7 @@ int runDPCT(int argc, const char **argv) {
10661067
DpctGlobalInfo::setExcludePath(ExcludePathList);
10671068
}
10681069

1069-
std::set<ExplicitNamespace> ExplicitNamespaces;
1070+
std::set<clang::dpct::ExplicitNamespace> ExplicitNamespaces;
10701071
if (UseExplicitNamespace.getNumOccurrences()) {
10711072
ExplicitNamespaces.insert(UseExplicitNamespace.begin(),
10721073
UseExplicitNamespace.end());

clang/lib/DPCT/Diagnostics/Diagnostics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
extern clang::dpct::DpctOption<clang::dpct::list, std::string> SuppressWarnings;
2929
extern clang::dpct::DpctOption<clang::dpct::opt, std::string> OutputFile;
30-
extern clang::dpct::DpctOption<clang::dpct::opt, OutputVerbosityLevel> OutputVerbosity;
30+
extern clang::dpct::DpctOption<clang::dpct::opt, clang::dpct::OutputVerbosityLevel> OutputVerbosity;
3131
extern clang::dpct::DpctOption<clang::dpct::opt, bool> SuppressWarningsAll;
3232

3333
namespace clang {

clang/lib/DPCT/ErrorHandle/Error.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,28 @@ std::string getCheckVersionFailWarning() {
247247
"details.\n";
248248
}
249249

250+
bool IsUsingDefaultOutRoot = false;
251+
252+
void removeDefaultOutRootFolder(const clang::tooling::UnifiedPath &DefaultOutRoot) {
253+
if (isDirectory(DefaultOutRoot)) {
254+
std::error_code EC;
255+
llvm::sys::fs::directory_iterator Iter(DefaultOutRoot.getCanonicalPath(), EC);
256+
if ((bool)EC)
257+
return;
258+
llvm::sys::fs::directory_iterator End;
259+
if (Iter == End) {
260+
// This folder is empty, then remove it.
261+
llvm::sys::fs::remove_directories(DefaultOutRoot.getCanonicalPath(), false);
262+
}
263+
}
264+
}
265+
266+
void dpctExit(int ExitCode, bool NeedCleanUp) {
267+
if (IsUsingDefaultOutRoot && NeedCleanUp) {
268+
removeDefaultOutRootFolder(dpct::DpctGlobalInfo::getOutRoot());
269+
}
270+
std::exit(ExitCode);
271+
}
272+
250273
} // namespace dpct
251274
} // namespace clang

clang/lib/DPCT/ErrorHandle/Error.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ namespace dpct {
7575
void ShowStatus(int Status, std::string Message = "");
7676
std::string getLoadYamlFailWarning(const clang::tooling::UnifiedPath& YamlPath);
7777
std::string getCheckVersionFailWarning();
78+
79+
extern bool IsUsingDefaultOutRoot;
80+
void removeDefaultOutRootFolder(const clang::tooling::UnifiedPath &DefaultOutRoot);
81+
void dpctExit(int ExitCode, bool NeedCleanUp = true);
82+
83+
7884
} // namespace dpct
7985
} // namespace clang
8086

clang/lib/DPCT/FileGenerator/GenFiles.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,29 @@ UnifiedPath getFormatSearchPath();
5050
} // namespace tooling
5151
} // namespace clang
5252

53+
5354
extern std::map<std::string, uint64_t> ErrorCnt;
5455

56+
/// Calculate the ranges of the input \p Ranges after \p Repls is applied to
57+
/// the files.
58+
/// \param Repls Replacements to apply.
59+
/// \param Ranges Ranges before applying the replacements.
60+
/// \return The result ranges.
61+
std::vector<clang::tooling::Range>
62+
calculateUpdatedRanges(const clang::tooling::Replacements &Repls,
63+
const std::vector<clang::tooling::Range> &Ranges) {
64+
std::vector<clang::tooling::Range> Result;
65+
for (const auto &R : Ranges) {
66+
unsigned int BOffset = Repls.getShiftedCodePosition(R.getOffset());
67+
unsigned int EOffset =
68+
Repls.getShiftedCodePosition(R.getOffset() + R.getLength());
69+
if (BOffset > EOffset)
70+
continue;
71+
Result.emplace_back(BOffset, EOffset - BOffset);
72+
}
73+
return Result;
74+
}
75+
5576
static bool formatFile(const clang::tooling::UnifiedPath &FileName,
5677
const std::vector<clang::tooling::Range> &Ranges,
5778
clang::tooling::Replacements &FormatChanges) {

0 commit comments

Comments
 (0)