diff --git a/clang/lib/DPCT/ASTTraversal.cpp b/clang/lib/DPCT/ASTTraversal.cpp index ed99858ad2ca..21189ddd8a01 100644 --- a/clang/lib/DPCT/ASTTraversal.cpp +++ b/clang/lib/DPCT/ASTTraversal.cpp @@ -35,6 +35,9 @@ using namespace clang; using namespace clang::dpct; +namespace clang { +namespace dpct { + unsigned MigrationRule::PairID = 0; void MigrationRule::print(llvm::raw_ostream &OS) { @@ -179,3 +182,6 @@ REGISTER_RULE(FFTFunctionCallRule, PassKind::PK_Migration,RuleGroupKind::RK_FFT) REGISTER_RULE(CuDNNTypeRule, PassKind::PK_Migration, RuleGroupKind::RK_DNN) REGISTER_RULE(CuDNNAPIRule, PassKind::PK_Migration, RuleGroupKind::RK_DNN) + +} // namespace dpct +} // namespace clang \ No newline at end of file diff --git a/clang/lib/DPCT/FileGenerator/GenFiles.cpp b/clang/lib/DPCT/FileGenerator/GenFiles.cpp index 88580cf56b97..a9e46f4d0564 100644 --- a/clang/lib/DPCT/FileGenerator/GenFiles.cpp +++ b/clang/lib/DPCT/FileGenerator/GenFiles.cpp @@ -1,4 +1,4 @@ -//===--------------- GenFiles.cpp -------------------------------------===// +//===--------------- GenFiles.cpp ----------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -44,14 +44,17 @@ using namespace llvm; namespace path = llvm::sys::path; namespace fs = llvm::sys::fs; +extern DpctOption ProcessAll; +extern DpctOption BuildScriptFile; +extern DpctOption GenBuildScript; +extern std::map ErrorCnt; + namespace clang { namespace tooling { UnifiedPath getFormatSearchPath(); } // namespace tooling -} // namespace clang - -extern std::map ErrorCnt; +namespace dpct{ /// Calculate the ranges of the input \p Ranges after \p Repls is applied to /// the files. @@ -294,7 +297,6 @@ void copyFileToOutRoot(clang::tooling::UnifiedPath &InRoot, void processallOptionAction(clang::tooling::UnifiedPath &InRoot, clang::tooling::UnifiedPath &OutRoot, bool IsForSYCL) { - extern DpctOption ProcessAll; if (ProcessAll) { std::error_code EC; for (fs::recursive_directory_iterator Iter(Twine(InRoot.getPath()), EC), @@ -440,8 +442,6 @@ void processAllFiles(StringRef InRoot, StringRef OutRoot, } } -extern DpctOption BuildScriptFile; -extern DpctOption GenBuildScript; static void getMainSrcFilesRepls( std::vector &MainSrcFilesRepls) { @@ -938,7 +938,6 @@ int saveNewFiles(clang::tooling::RefactoringTool &Tool, SourceManager Sources(Diagnostics, Tool.getFiles()); Rewriter Rewrite(Sources, DefaultLangOptions); Rewriter DebugCUDARewrite(Sources, DefaultLangOptions); - extern DpctOption ProcessAll; // The variable defined here assists to merge history records. std::unordered_map OutFilePath2InFilePath; + +} // namespace dpct +} // namespace clang + #endif // DPCT_GEN_FILES_H diff --git a/clang/lib/DPCT/IncMigration/ExternalReplacement.cpp b/clang/lib/DPCT/IncMigration/ExternalReplacement.cpp index 49949a0f9f4b..1b88275985de 100644 --- a/clang/lib/DPCT/IncMigration/ExternalReplacement.cpp +++ b/clang/lib/DPCT/IncMigration/ExternalReplacement.cpp @@ -35,6 +35,8 @@ namespace path = llvm::sys::path; namespace fs = llvm::sys::fs; using clang::tooling::Replacements; +namespace clang { +namespace dpct { int save2Yaml( clang::tooling::UnifiedPath &YamlFile, clang::tooling::UnifiedPath &SrcFileName, @@ -163,3 +165,5 @@ int mergeExternalReps(clang::tooling::UnifiedPath InRootSrcFilePath, save2Yaml(YamlFile, OutRootSrcFilePath, Repls, {MsfInfo}, CompileTargets); return 0; } +} // namespace dpct +} // namespace clang \ No newline at end of file diff --git a/clang/lib/DPCT/IncMigration/ExternalReplacement.h b/clang/lib/DPCT/IncMigration/ExternalReplacement.h index e327149b75f2..20e819d16fc1 100644 --- a/clang/lib/DPCT/IncMigration/ExternalReplacement.h +++ b/clang/lib/DPCT/IncMigration/ExternalReplacement.h @@ -25,6 +25,9 @@ class Replacements; } // namespace tooling } // namespace clang +namespace clang { +namespace dpct { + int mergeExternalReps(clang::tooling::UnifiedPath InRootSrcFilePath, clang::tooling::UnifiedPath OutRootSrcFilePath, clang::tooling::Replacements &Replaces); @@ -43,4 +46,7 @@ void mergeAndUniqueReps( clang::tooling::Replacements &Replaces, const std::vector &PreRepls); -#endif +} // namespace dpct +} // namespace clang + +#endif // __EXTERNAL_REPLACEMENT_H__ diff --git a/clang/lib/DPCT/MigrateScript/GenMakefile.cpp b/clang/lib/DPCT/MigrateScript/GenMakefile.cpp index 6cf166a9a149..086bd68b5715 100644 --- a/clang/lib/DPCT/MigrateScript/GenMakefile.cpp +++ b/clang/lib/DPCT/MigrateScript/GenMakefile.cpp @@ -30,6 +30,8 @@ using namespace clang::dpct; using namespace llvm; +namespace clang { +namespace dpct { // Used to identify compilation commands without target const std::string EmptyTarget = "NoLinker"; @@ -679,3 +681,5 @@ void genBuildScript(clang::tooling::RefactoringTool &Tool, genMakefile(Tool, OutRoot, BuildScriptName, CompileCmdsPerTarget, ToolPerTarget); } +} // namespace dpct +} // namespace clang \ No newline at end of file diff --git a/clang/lib/DPCT/MigrateScript/GenMakefile.h b/clang/lib/DPCT/MigrateScript/GenMakefile.h index 165b92789463..2bd6392e67dc 100644 --- a/clang/lib/DPCT/MigrateScript/GenMakefile.h +++ b/clang/lib/DPCT/MigrateScript/GenMakefile.h @@ -25,6 +25,8 @@ class RefactoringTool; } } // namespace clang +namespace clang { +namespace dpct { /// Generates makefile for migrated file(s) in -out-root directory. /// The name of generated makefile is specified by \p BuildScriptName void genBuildScript(clang::tooling::RefactoringTool &Tool, @@ -39,5 +41,6 @@ extern std::vector< std::pair /*orginal compile command*/>> CompileTargetsMap; - +} // namespace dpct +} // namespace clang #endif // DPCT_GEN_MAKEFILE_H diff --git a/clang/lib/DPCT/MigrateScript/MigrateCmakeScript.cpp b/clang/lib/DPCT/MigrateScript/MigrateCmakeScript.cpp index c0d6df3d1a17..35fc204b3152 100644 --- a/clang/lib/DPCT/MigrateScript/MigrateCmakeScript.cpp +++ b/clang/lib/DPCT/MigrateScript/MigrateCmakeScript.cpp @@ -28,6 +28,8 @@ using namespace llvm::cl; namespace path = llvm::sys::path; namespace fs = llvm::sys::fs; +namespace clang { +namespace dpct { std::map> cmake_commands{ @@ -576,3 +578,6 @@ void registerCmakeMigrationRule(MetaRuleObject &R) { CmakeBuildInRules[PR.BuildScriptSyntax] = PR; } } + +} // namespace dpct +} // namespace clang \ No newline at end of file diff --git a/clang/lib/DPCT/MigrateScript/MigrateCmakeScript.h b/clang/lib/DPCT/MigrateScript/MigrateCmakeScript.h index a8a7ebe4cf7c..e42d087f3b4a 100644 --- a/clang/lib/DPCT/MigrateScript/MigrateCmakeScript.h +++ b/clang/lib/DPCT/MigrateScript/MigrateCmakeScript.h @@ -11,6 +11,8 @@ #include "MigrateBuildScript.h" #include "UserDefinedRules/UserDefinedRules.h" +namespace clang { +namespace dpct { void collectCmakeScripts(const clang::tooling::UnifiedPath &InRoot, const clang::tooling::UnifiedPath &OutRoot); void collectCmakeScriptsSpecified( @@ -23,4 +25,7 @@ void registerCmakeMigrationRule(MetaRuleObject &R); bool cmakeScriptNotFound(); void addCmakeWarningMsg(const std::string &WarningMsg, const std::string FileName); -#endif + +} // namespace dpct +} // namespace clang +#endif //DPCT_MIGRATE_CMAKE_SCRIPT_H diff --git a/clang/lib/DPCT/MigrateScript/MigratePythonBuildScript.cpp b/clang/lib/DPCT/MigrateScript/MigratePythonBuildScript.cpp index 1843bb42d9e4..05880b5ad4be 100644 --- a/clang/lib/DPCT/MigrateScript/MigratePythonBuildScript.cpp +++ b/clang/lib/DPCT/MigrateScript/MigratePythonBuildScript.cpp @@ -28,6 +28,8 @@ using namespace llvm::cl; namespace path = llvm::sys::path; namespace fs = llvm::sys::fs; +namespace clang { +namespace dpct { static std::vector PythonBuildScriptFilesSet; static std::map ProcessAll; +namespace clang { +namespace dpct { + + std::shared_ptr generateReplacement(SourceLocation SL, MacroMigrationRule &Rule) { @@ -815,4 +819,7 @@ void IncludesCallbacks::FileChanged(SourceLocation Loc, FileChangeReason Reason, loadYAMLIntoFileInfo(InFile.getCanonicalPath()); } -} \ No newline at end of file +} + +} // namespace dpct +} // namespace clang \ No newline at end of file diff --git a/clang/lib/DPCT/RuleInfra/ASTmatcherCommon.h b/clang/lib/DPCT/RuleInfra/ASTmatcherCommon.h new file mode 100644 index 000000000000..c17bf2388764 --- /dev/null +++ b/clang/lib/DPCT/RuleInfra/ASTmatcherCommon.h @@ -0,0 +1,36 @@ +//===--------------- ASTmatcherCommon.h ----------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef DPCT_AST_MATCHER_COMMON_H +#define DPCT_AST_MATCHER_COMMON_H +#include "clang/ASTMatchers/ASTMatchersInternal.h" + +using namespace clang::ast_matchers; +namespace clang { +namespace dpct { + +inline auto parentStmtCub = []() { + return anyOf(hasParent(compoundStmt()), hasParent(forStmt()), + hasParent(whileStmt()), hasParent(doStmt()), + hasParent(ifStmt())); +}; + +inline auto parentStmt = []() { + return anyOf( + hasParent(compoundStmt()), hasParent(forStmt()), hasParent(whileStmt()), + hasParent(doStmt()), hasParent(ifStmt()), + hasParent(exprWithCleanups(anyOf( + hasParent(compoundStmt()), hasParent(forStmt()), + hasParent(whileStmt()), hasParent(doStmt()), hasParent(ifStmt()))))); +}; + + +} //namespace dpct +} //namespace clang + +#endif //!DPCT_AST_MATCHER_COMMON_H \ No newline at end of file diff --git a/clang/lib/DPCT/RuleInfra/CallExprRewriterCommon.h b/clang/lib/DPCT/RuleInfra/CallExprRewriterCommon.h index baa21aa1f6bf..0d88a3ea482b 100644 --- a/clang/lib/DPCT/RuleInfra/CallExprRewriterCommon.h +++ b/clang/lib/DPCT/RuleInfra/CallExprRewriterCommon.h @@ -958,9 +958,9 @@ inline std::function checkArgSpelling(size_t index, inline std::function checkIsCallExprOnly() { return [=](const CallExpr *C) -> bool { - auto parentStmt = getParentStmt(C); - if (parentStmt != nullptr && (dyn_cast(parentStmt) || - dyn_cast(parentStmt))) + auto ParentStmt = getParentStmt(C); + if (ParentStmt != nullptr && (dyn_cast(ParentStmt) || + dyn_cast(ParentStmt))) return true; return false; }; diff --git a/clang/lib/DPCT/RulesLang/RulesLang.cpp b/clang/lib/DPCT/RulesLang/RulesLang.cpp index dec22332c763..6dd5260b0213 100644 --- a/clang/lib/DPCT/RulesLang/RulesLang.cpp +++ b/clang/lib/DPCT/RulesLang/RulesLang.cpp @@ -14,6 +14,7 @@ #include "RulesLang/BarrierFenceSpaceAnalyzer.h" #include "RuleInfra/CallExprRewriter.h" #include "RuleInfra/CallExprRewriterCommon.h" +#include "RuleInfra/ASTmatcherCommon.h" #include "RulesDNN/DNNAPIMigration.h" #include "RuleInfra/ExprAnalysis.h" #include "RulesMathLib/FFTAPIMigration.h" @@ -66,15 +67,12 @@ using namespace clang::tooling; extern clang::tooling::UnifiedPath DpctInstallPath; // Installation directory for this tool extern DpctOption ProcessAll; +extern DpctOption AsyncHandler; + +namespace clang{ +namespace dpct{ + -auto parentStmt = []() { - return anyOf( - hasParent(compoundStmt()), hasParent(forStmt()), hasParent(whileStmt()), - hasParent(doStmt()), hasParent(ifStmt()), - hasParent(exprWithCleanups(anyOf( - hasParent(compoundStmt()), hasParent(forStmt()), - hasParent(whileStmt()), hasParent(doStmt()), hasParent(ifStmt()))))); -}; static const CXXConstructorDecl *getIfConstructorDecl(const Decl *ND) { if (const auto *Tmpl = dyn_cast(ND)) @@ -2012,6 +2010,8 @@ void VectorTypeMemberAccessRule::runRule( } } +} //namespace clang +} //namespace dpct namespace clang { namespace ast_matchers { @@ -2071,6 +2071,9 @@ AST_MATCHER(FunctionDecl, overloadedVectorOperator) { } // namespace ast_matchers } // namespace clang +namespace clang { +namespace dpct { + void VectorTypeOperatorRule::registerMatcher(MatchFinder &MF) { auto vectorTypeOverLoadedOperator = [&]() { return functionDecl(overloadedVectorOperator(), @@ -4679,7 +4682,6 @@ void StreamAPICallRule::registerMatcher(MatchFinder &MF) { } std::string getNewQueue(int Index) { - extern DpctOption AsyncHandler; std::string Result; llvm::raw_string_ostream OS(Result); printPartialArguments(OS << "{{NEEDREPLACED" << std::to_string(Index) @@ -6502,7 +6504,7 @@ void MemoryMigrationRule::mallocMigrationWithTransformation( /// e.g., for int *a and cudaMalloc(&a, size), print "a = ". /// If \p DerefType is not null, assign a string "int *". -void clang::dpct::printDerefOp(std::ostream &OS, const Expr *E, std::string *DerefType) { +void printDerefOp(std::ostream &OS, const Expr *E, std::string *DerefType) { E = E->IgnoreImplicitAsWritten(); bool NeedDerefOp = true; if (auto UO = dyn_cast(E)) { @@ -11803,3 +11805,6 @@ void GraphicsInteropRule::runRule( emplaceTransformation(EA.getReplacement()); EA.applyAllSubExprRepl(); } + +} //namespace clang +} //namespace dpct \ No newline at end of file diff --git a/clang/lib/DPCT/RulesLangLib/CUBAPIMigration.cpp b/clang/lib/DPCT/RulesLangLib/CUBAPIMigration.cpp index 2bcc88625eee..97777aaa3aa5 100644 --- a/clang/lib/DPCT/RulesLangLib/CUBAPIMigration.cpp +++ b/clang/lib/DPCT/RulesLangLib/CUBAPIMigration.cpp @@ -11,6 +11,7 @@ #include "TextModification.h" #include "AnalysisInfo.h" #include "RuleInfra/CallExprRewriter.h" +#include "RuleInfra/ASTmatcherCommon.h" #include "RuleInfra/ExprAnalysis.h" #include "MigrationRuleManager.h" #include "TextModification.h" @@ -54,11 +55,7 @@ using namespace tooling; using namespace ast_matchers; namespace { -auto parentStmt = []() { - return anyOf(hasParent(compoundStmt()), hasParent(forStmt()), - hasParent(whileStmt()), hasParent(doStmt()), - hasParent(ifStmt())); -}; + auto isDeviceFuncCallExpr = []() { auto hasDeviceFuncName = []() { @@ -767,7 +764,7 @@ void CubRule::registerMatcher(ast_matchers::MatchFinder &MF) { "ShuffleIndex", "ThreadLoad", "ThreadStore", "Sum", "Reduce", "ExclusiveSum", "InclusiveSum", "InclusiveScan", "ExclusiveScan"))), - parentStmt())) + parentStmtCub())) .bind("FuncCall"), this); @@ -776,7 +773,7 @@ void CubRule::registerMatcher(ast_matchers::MatchFinder &MF) { "Sum", "Reduce", "ThreadLoad", "ShuffleIndex", "ExclusiveSum", "InclusiveSum", "InclusiveScan", "ExclusiveScan"))), - unless(parentStmt()))) + unless(parentStmtCub()))) .bind("FuncCallUsed"), this); } diff --git a/clang/lib/DPCT/RulesMathLib/BLASAPIMigration.cpp b/clang/lib/DPCT/RulesMathLib/BLASAPIMigration.cpp index 57b5acbef04f..66a4727c6990 100644 --- a/clang/lib/DPCT/RulesMathLib/BLASAPIMigration.cpp +++ b/clang/lib/DPCT/RulesMathLib/BLASAPIMigration.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "BLASAPIMigration.h" +#include "RuleInfra/ASTmatcherCommon.h" namespace clang { namespace dpct { @@ -56,14 +57,7 @@ bool checkConstQualifierInDoublePointerType( return false; } -auto parentStmt = []() { - return anyOf( - hasParent(compoundStmt()), hasParent(forStmt()), hasParent(whileStmt()), - hasParent(doStmt()), hasParent(ifStmt()), - hasParent(exprWithCleanups(anyOf( - hasParent(compoundStmt()), hasParent(forStmt()), - hasParent(whileStmt()), hasParent(doStmt()), hasParent(ifStmt()))))); -}; + // Rule for BLAS enums. // Migrate BLAS status values to corresponding int values diff --git a/clang/lib/DPCT/RulesMathLib/RandomAPIMigration.cpp b/clang/lib/DPCT/RulesMathLib/RandomAPIMigration.cpp index c64e36014e77..9ff1f459f043 100644 --- a/clang/lib/DPCT/RulesMathLib/RandomAPIMigration.cpp +++ b/clang/lib/DPCT/RulesMathLib/RandomAPIMigration.cpp @@ -9,6 +9,7 @@ #include "RandomAPIMigration.h" #include "RuleInfra/CallExprRewriter.h" #include "RuleInfra/CallExprRewriterCommon.h" +#include "RuleInfra/ASTmatcherCommon.h" namespace clang { namespace dpct { @@ -68,14 +69,6 @@ void RandomEnumsRule::runRule(const MatchFinder::MatchResult &Result) { // Rule for Random function calls. Currently only support host APIs. void RandomFunctionCallRule::registerMatcher(MatchFinder &MF) { - auto parentStmt = []() { - return anyOf( - hasParent(compoundStmt()), hasParent(forStmt()), hasParent(whileStmt()), - hasParent(doStmt()), hasParent(ifStmt()), - hasParent(exprWithCleanups(anyOf( - hasParent(compoundStmt()), hasParent(forStmt()), - hasParent(whileStmt()), hasParent(doStmt()), hasParent(ifStmt()))))); - }; auto functionName = [&]() { return hasAnyName( "curandCreateGenerator", "curandSetPseudoRandomGeneratorSeed", diff --git a/clang/lib/DPCT/RulesMathLib/SolverAPIMigration.cpp b/clang/lib/DPCT/RulesMathLib/SolverAPIMigration.cpp index 449980f5c768..c0c1bb10401f 100644 --- a/clang/lib/DPCT/RulesMathLib/SolverAPIMigration.cpp +++ b/clang/lib/DPCT/RulesMathLib/SolverAPIMigration.cpp @@ -10,6 +10,7 @@ #include "RuleInfra/CallExprRewriter.h" #include "RuleInfra/CallExprRewriterCommon.h" #include "RulesLang/RulesLang.h" +#include "RuleInfra/ASTmatcherCommon.h" namespace clang { namespace dpct { @@ -56,14 +57,7 @@ void SOLVEREnumsRule::runRule(const MatchFinder::MatchResult &Result) { void SOLVERFunctionCallRule::registerMatcher(MatchFinder &MF) { - auto parentStmt = []() { - return anyOf( - hasParent(compoundStmt()), hasParent(forStmt()), hasParent(whileStmt()), - hasParent(doStmt()), hasParent(ifStmt()), - hasParent(exprWithCleanups(anyOf( - hasParent(compoundStmt()), hasParent(forStmt()), - hasParent(whileStmt()), hasParent(doStmt()), hasParent(ifStmt()))))); - }; + auto functionName = [&]() { return hasAnyName( "cusolverDnSetAdvOptions", "cusolverDnGetStream", "cusolverDnSetStream", diff --git a/clang/lib/DPCT/RulesMathLib/SpBLASAPIMigration.cpp b/clang/lib/DPCT/RulesMathLib/SpBLASAPIMigration.cpp index 2e4c246947a0..550d6c424312 100644 --- a/clang/lib/DPCT/RulesMathLib/SpBLASAPIMigration.cpp +++ b/clang/lib/DPCT/RulesMathLib/SpBLASAPIMigration.cpp @@ -9,6 +9,7 @@ #include "SpBLASAPIMigration.h" #include "RuleInfra/CallExprRewriter.h" #include "RuleInfra/CallExprRewriterCommon.h" +#include "RuleInfra/ASTmatcherCommon.h" namespace clang { namespace dpct { @@ -39,14 +40,7 @@ void SpBLASTypeLocRule::runRule( // Rule for spBLAS function calls. void SPBLASFunctionCallRule::registerMatcher(MatchFinder &MF) { - auto parentStmt = []() { - return anyOf( - hasParent(compoundStmt()), hasParent(forStmt()), hasParent(whileStmt()), - hasParent(doStmt()), hasParent(ifStmt()), - hasParent(exprWithCleanups(anyOf( - hasParent(compoundStmt()), hasParent(forStmt()), - hasParent(whileStmt()), hasParent(doStmt()), hasParent(ifStmt()))))); - }; + auto functionName = [&]() { return hasAnyName( diff --git a/clang/lib/DPCT/TextModification.cpp b/clang/lib/DPCT/TextModification.cpp index 010b5cd64ce0..dbd0c23ad4bc 100644 --- a/clang/lib/DPCT/TextModification.cpp +++ b/clang/lib/DPCT/TextModification.cpp @@ -25,6 +25,9 @@ using namespace clang; using namespace clang::dpct; using namespace clang::tooling; +namespace clang { +namespace dpct { + bool ReplaceStmt::inCompoundStmt(const Stmt *E) { auto &context = DpctGlobalInfo::getContext(); const auto parents = context.getParents(*E); @@ -100,7 +103,7 @@ ReplaceStmt::getReplacement(const ASTContext &Context) const { ReplacementString.empty() && !IsSingleLineStatement(TheStmt)) { return removeStmtWithCleanups(SM); } - auto &Context = dpct::DpctGlobalInfo::getContext(); + auto &Context = DpctGlobalInfo::getContext(); auto LastTokenLength = Lexer::MeasureTokenLength(End, SM, Context.getLangOpts()); auto CallExprLength = SM.getDecomposedLoc(End).second - @@ -340,7 +343,7 @@ ReplaceVarDecl::getReplacement(const ASTContext &Context) const { SourceLocation Loc = D->getEndLoc(); while (true) { auto Tok = Lexer::findNextToken( - Loc, SM, dpct::DpctGlobalInfo::getContext().getLangOpts()); + Loc, SM, DpctGlobalInfo::getContext().getLangOpts()); if (Tok.has_value()) { auto Val = Tok.value(); Loc = Tok.value().getLocation(); @@ -867,7 +870,7 @@ void ReplaceText::print(llvm::raw_ostream &OS, ASTContext &Context, printInsertion(OS, T); } -TextModification *clang::dpct::replaceText(SourceLocation Begin, SourceLocation End, +TextModification * replaceText(SourceLocation Begin, SourceLocation End, std::string &&Str, const SourceManager &SM) { auto Length = SM.getFileOffset(End) - SM.getFileOffset(Begin); if (Length > 0) { @@ -875,7 +878,7 @@ TextModification *clang::dpct::replaceText(SourceLocation Begin, SourceLocation } return nullptr; } -SourceLocation clang::dpct::getArgEndLocation(const CallExpr *C, unsigned Idx, +SourceLocation getArgEndLocation(const CallExpr *C, unsigned Idx, const SourceManager &SM) { auto SL = getStmtExpansionSourceRange(C->getArg(Idx)).getEnd(); return SL.getLocWithOffset(Lexer::MeasureTokenLength( @@ -884,7 +887,7 @@ SourceLocation clang::dpct::getArgEndLocation(const CallExpr *C, unsigned Idx, /// Return a TextModication that removes nth argument of the CallExpr, /// together with the preceding comma. -TextModification *clang::dpct::removeArg(const CallExpr *C, unsigned n, +TextModification * removeArg(const CallExpr *C, unsigned n, const SourceManager &SM) { if (C->getNumArgs() <= n) return nullptr; @@ -905,3 +908,5 @@ TextModification *clang::dpct::removeArg(const CallExpr *C, unsigned n, } return replaceText(Begin, End, "", SM); } +} // namespace dpct +} // namespace clang \ No newline at end of file diff --git a/clang/lib/DPCT/UserDefinedRules/UserDefinedRules.cpp b/clang/lib/DPCT/UserDefinedRules/UserDefinedRules.cpp index 987b84148114..c19462924e54 100644 --- a/clang/lib/DPCT/UserDefinedRules/UserDefinedRules.cpp +++ b/clang/lib/DPCT/UserDefinedRules/UserDefinedRules.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// #include "UserDefinedRules/UserDefinedRules.h" +#include "RuleInfra/ASTmatcherCommon.h" #include "ASTTraversal.h" #include "RulesLang/RulesLang.h" #include "RuleInfra/CallExprRewriter.h" @@ -22,6 +23,8 @@ using namespace clang::ast_matchers; using namespace clang::dpct; +namespace clang { +namespace dpct { std::vector MetaRuleObject::RuleFiles; std::vector> MetaRules; @@ -782,3 +785,6 @@ void clang::dpct::UserDefinedEnumRule::runRule( EA.applyAllSubExprRepl(); } } + +} // namespace dpct +} // namespace clang \ No newline at end of file diff --git a/clang/lib/DPCT/UserDefinedRules/UserDefinedRules.h b/clang/lib/DPCT/UserDefinedRules/UserDefinedRules.h index d18eefb6adcc..baddd23a4f8c 100644 --- a/clang/lib/DPCT/UserDefinedRules/UserDefinedRules.h +++ b/clang/lib/DPCT/UserDefinedRules/UserDefinedRules.h @@ -14,6 +14,9 @@ #include #include +namespace clang { +namespace dpct { + enum RuleKind { API, DataType, @@ -148,6 +151,95 @@ class MetaRuleObject { } }; +class RuleBase { +public: + std::string Id; + RulePriority Priority; + RuleMatchMode MatchMode; + RuleKind Kind; + std::string In; + std::string Out; + clang::dpct::HelperFeatureEnum HelperFeature; + std::vector Includes; + + RuleBase( + std::string Id, RulePriority Priority, RuleKind Kind, std::string In, + std::string Out, clang::dpct::HelperFeatureEnum HelperFeature, + const std::vector &Includes = std::vector()) + : Id(Id), Priority(Priority), MatchMode(RuleMatchMode::Partial), + Kind(Kind), In(In), Out(Out), HelperFeature(HelperFeature), + Includes(Includes) {} +}; + +class MacroMigrationRule : public RuleBase { +public: + MacroMigrationRule( + std::string Id, RulePriority Priority, std::string InStr, + std::string OutStr, + clang::dpct::HelperFeatureEnum Helper = + clang::dpct::HelperFeatureEnum::none, + const std::vector &Includes = std::vector()) + : RuleBase(Id, Priority, RuleKind::Macro, InStr, OutStr, Helper, + Includes) {} +}; + +// The parsing result of the "Out" attribute of a API rule +// Kind::Top labels the root node. +// For example, if the input "Out" string is: +// foo($1, $deref($2)) +// The SubBuilders of the "Top" OutputBuilder will be: +// 1. OutputBuilder: Kind="String", Str="foo(" +// 2. OutputBuilder: Kind = "Arg", ArgIndex=1 +// 3. OutputBuilder: Kind = "Deref", ArgIndex=2 +// 4. OutputBuilder: Kind = "String", Str=")" +class OutputBuilder { +public: + enum Kind { + String, + Top, + Arg, + Queue, + Context, + Device, + Deref, + TypeName, + AddrOf, + DerefedTypeName, + TemplateArg, + MethodBase + }; + std::string RuleName; + clang::tooling::UnifiedPath RuleFile; + Kind Kind; + size_t ArgIndex; + std::string Str; + std::vector> SubBuilders; + void parse(std::string &); + virtual ~OutputBuilder(); +protected: + // /OutStr is the string specified in rule's "Out" session + virtual std::shared_ptr consumeKeyword(std::string &OutStr, + size_t &Idx); + int consumeArgIndex(std::string &OutStr, size_t &Idx, std::string &&Keyword); + void ignoreWhitespaces(std::string &OutStr, size_t &Idx); + void consumeRParen(std::string &OutStr, size_t &Idx, std::string &&Keyword); + void consumeLParen(std::string &OutStr, size_t &Idx, std::string &&Keyword); +}; + +class TypeOutputBuilder : public OutputBuilder { +private: + // /OutStr is the string specified in rule's "Out" session + std::shared_ptr consumeKeyword(std::string &OutStr, + size_t &Idx) override; +}; + +void importRules(std::vector &RuleFiles); + +} // namespace dpct +} // namespace clang + + +using namespace clang::dpct; template <> struct llvm::yaml::CustomMappingTraits< std::map> { @@ -262,7 +354,7 @@ struct llvm::yaml::MappingTraits> { }; template <> -struct llvm::yaml::MappingTraits> { +struct llvm::yaml::MappingTraits> { static void mapping(llvm::yaml::IO &Io, std::shared_ptr &Doc) { Doc = std::make_shared(); @@ -293,88 +385,4 @@ struct llvm::yaml::MappingTraits { } }; -class RuleBase { -public: - std::string Id; - RulePriority Priority; - RuleMatchMode MatchMode; - RuleKind Kind; - std::string In; - std::string Out; - clang::dpct::HelperFeatureEnum HelperFeature; - std::vector Includes; - - RuleBase( - std::string Id, RulePriority Priority, RuleKind Kind, std::string In, - std::string Out, clang::dpct::HelperFeatureEnum HelperFeature, - const std::vector &Includes = std::vector()) - : Id(Id), Priority(Priority), MatchMode(RuleMatchMode::Partial), - Kind(Kind), In(In), Out(Out), HelperFeature(HelperFeature), - Includes(Includes) {} -}; - -class MacroMigrationRule : public RuleBase { -public: - MacroMigrationRule( - std::string Id, RulePriority Priority, std::string InStr, - std::string OutStr, - clang::dpct::HelperFeatureEnum Helper = - clang::dpct::HelperFeatureEnum::none, - const std::vector &Includes = std::vector()) - : RuleBase(Id, Priority, RuleKind::Macro, InStr, OutStr, Helper, - Includes) {} -}; - -// The parsing result of the "Out" attribute of a API rule -// Kind::Top labels the root node. -// For example, if the input "Out" string is: -// foo($1, $deref($2)) -// The SubBuilders of the "Top" OutputBuilder will be: -// 1. OutputBuilder: Kind="String", Str="foo(" -// 2. OutputBuilder: Kind = "Arg", ArgIndex=1 -// 3. OutputBuilder: Kind = "Deref", ArgIndex=2 -// 4. OutputBuilder: Kind = "String", Str=")" -class OutputBuilder { -public: - enum Kind { - String, - Top, - Arg, - Queue, - Context, - Device, - Deref, - TypeName, - AddrOf, - DerefedTypeName, - TemplateArg, - MethodBase - }; - std::string RuleName; - clang::tooling::UnifiedPath RuleFile; - Kind Kind; - size_t ArgIndex; - std::string Str; - std::vector> SubBuilders; - void parse(std::string &); - virtual ~OutputBuilder(); -protected: - // /OutStr is the string specified in rule's "Out" session - virtual std::shared_ptr consumeKeyword(std::string &OutStr, - size_t &Idx); - int consumeArgIndex(std::string &OutStr, size_t &Idx, std::string &&Keyword); - void ignoreWhitespaces(std::string &OutStr, size_t &Idx); - void consumeRParen(std::string &OutStr, size_t &Idx, std::string &&Keyword); - void consumeLParen(std::string &OutStr, size_t &Idx, std::string &&Keyword); -}; - -class TypeOutputBuilder : public OutputBuilder { -private: - // /OutStr is the string specified in rule's "Out" session - std::shared_ptr consumeKeyword(std::string &OutStr, - size_t &Idx) override; -}; - -void importRules(std::vector &RuleFiles); - #endif // DPCT_USER_DEFINED_RULES_H \ No newline at end of file