diff --git a/clang/lib/DPCT/ExtReplacements.cpp b/clang/lib/DPCT/ExtReplacements.cpp index 2d4e0eef7a96..ffeadf5b6295 100644 --- a/clang/lib/DPCT/ExtReplacements.cpp +++ b/clang/lib/DPCT/ExtReplacements.cpp @@ -305,6 +305,44 @@ void ExtReplacements::markAsAlive(std::shared_ptr Repl) { } } +void ExtReplacements::markAsDead(std::shared_ptr Repl) { + if (auto PairID = Repl->getPairID()) { + if (auto &R = PairReplsMap[PairID]) { + R->Status = PairReplsStatus::Dead; + auto BeginIter = ReplMap.lower_bound(R->Repl->getOffset()); + auto EndIter = ReplMap.upper_bound(R->Repl->getOffset()); + for (auto Start = BeginIter; Start != EndIter; Start++) { + if (*Start->second == *R->Repl) { + ReplMap.erase(Start); + break; + } + } + } else { + PairReplsMap[PairID] = + std::make_shared(Repl, PairReplsStatus::Dead); + } + } +} + +bool ExtReplacements::checkLiveness(std::shared_ptr Repl) { + if (isAlive(Repl)) + // If a replacement in the same pair is alive, merge it anyway. + return true; + // Check if it is duplicate replacement. + return !isDuplicated(Repl, ReplMap.lower_bound(Repl->getOffset()), + ReplMap.upper_bound(Repl->getOffset())); +} +// Check if its pair has a replacement inserted. +bool ExtReplacements::isAlive(std::shared_ptr Repl) { + if (auto PairID = Repl->getPairID()) { + if (auto &R = PairReplsMap[PairID]) { + if (!R->Repl->getReplacementText().contains( + "namespace dpct_operator_overloading {")) + return R->Status == PairReplsStatus::Alive; + } + } + return false; +} bool ExtReplacements::isDuplicated(std::shared_ptr Repl, ReplIterator Begin, ReplIterator End) { while (Begin != End) { diff --git a/clang/lib/DPCT/ExtReplacements.h b/clang/lib/DPCT/ExtReplacements.h index c24e9a04de44..3307689873de 100644 --- a/clang/lib/DPCT/ExtReplacements.h +++ b/clang/lib/DPCT/ExtReplacements.h @@ -66,14 +66,7 @@ class ExtReplacements { // Check if the Repl is same as source code bool isReplRedundant(std::shared_ptr Repl, std::shared_ptr FileInfo); - inline bool checkLiveness(std::shared_ptr Repl) { - if (isAlive(Repl)) - // If a replacement in the same pair is alive, merge it anyway. - return true; - // Check if it is duplicate replacement. - return !isDuplicated(Repl, ReplMap.lower_bound(Repl->getOffset()), - ReplMap.upper_bound(Repl->getOffset())); - } + bool checkLiveness(std::shared_ptr Repl); bool isDuplicated(std::shared_ptr Repl, ReplIterator Begin, ReplIterator End); @@ -136,11 +129,7 @@ class ExtReplacements { size_t findCR(StringRef Line); // Mark a replacement as dead. - void markAsDead(std::shared_ptr Repl) { - if (auto PairID = Repl->getPairID()) - PairReplsMap[PairID] = - std::make_shared(Repl, PairReplsStatus::Dead); - } + void markAsDead(std::shared_ptr Repl); // Mark a replacement as alive and insert into ReplMap // If it is not the first encountered replacement and the first one is @@ -148,13 +137,7 @@ class ExtReplacements { void markAsAlive(std::shared_ptr Repl); // Check if its pair has a replacement inserted. - bool isAlive(std::shared_ptr Repl) { - if (auto PairID = Repl->getPairID()) { - if (auto &R = PairReplsMap[PairID]) - return R->Status == PairReplsStatus::Alive; - } - return false; - } + bool isAlive(std::shared_ptr Repl); clang::tooling::UnifiedPath FilePath; ///< Offset, ExtReplacement> diff --git a/clang/lib/DPCT/RulesLang/RulesLang.cpp b/clang/lib/DPCT/RulesLang/RulesLang.cpp index 99b55946bf96..356bb0b1dcfd 100644 --- a/clang/lib/DPCT/RulesLang/RulesLang.cpp +++ b/clang/lib/DPCT/RulesLang/RulesLang.cpp @@ -1727,8 +1727,8 @@ void VectorTypeOperatorRule::MigrateOverloadedOperatorDecl( SR = GetFunctionSourceRange(SM, FD->getBeginLoc(), FD->getEndLoc()); } report(SR.getBegin(), Diagnostics::TRNA_WARNING_OVERLOADED_API_FOUND, false); - emplaceTransformation(new InsertText(SR.getBegin(), Prologue.str())); - emplaceTransformation(new InsertText(SR.getEnd(), Epilogue.str())); + insertAroundRange(SR.getBegin(), SR.getEnd(), Prologue.str(), + Epilogue.str()); } void VectorTypeOperatorRule::MigrateOverloadedOperatorCall( diff --git a/clang/test/dpct/insert_around/compile_commands.json b/clang/test/dpct/insert_around/compile_commands.json new file mode 100644 index 000000000000..c0235aac2e61 --- /dev/null +++ b/clang/test/dpct/insert_around/compile_commands.json @@ -0,0 +1,12 @@ +[ + { + "command": "nvcc -c -o float_vector.o ./float_vector.cpp", + "directory": ".", + "file": "./float_vector.cpp" + }, + { + "command": "c++ -c -o float_vector.o ./float_vector.cpp", + "directory": ".", + "file": "./float_vector.cpp" + } +] \ No newline at end of file diff --git a/clang/test/dpct/insert_around/float_vector.cpp b/clang/test/dpct/insert_around/float_vector.cpp new file mode 100644 index 000000000000..bf3b66e0b975 --- /dev/null +++ b/clang/test/dpct/insert_around/float_vector.cpp @@ -0,0 +1,21 @@ +// UNSUPPORTED: system-windows +// RUN: cd %T +// RUN: cat %S/compile_commands.json > %T/compile_commands.json +// RUN: cat %S/float_vector.cpp > %T/float_vector.cpp +// RUN: dpct -p %T -out-root %T/float_vector --cuda-include-path="%cuda-path/include" +// RUN: FileCheck %s --match-full-lines --input-file %T/float_vector/float_vector.cpp.dp.cpp + + +#include +namespace quda { + // CHECK: namespace dpct_operator_overloading { + // CHECK-EMPTY: + // CHECK-NEXT: inline sycl::double2 operator+(const sycl::double2 &x, const sycl::double2 &y) + __host__ __device__ inline double2 operator+(const double2 &x, const double2 &y) + { + } + // CHECK:} // namespace dpct_operator_overloading +} +int main() { + return 0; +}