From 855ec8dd11ba486f0fd13dbbb635a524173629f3 Mon Sep 17 00:00:00 2001 From: atimofee Date: Wed, 2 Mar 2016 17:22:06 +0300 Subject: [PATCH] String literals processing unified [ROCm/hip commit: f5fe77db4e5e4f19c8446d15073823e5e74249a2] --- projects/hip/src/Cuda2Hip.cpp | 60 ++++++++++++++++------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/projects/hip/src/Cuda2Hip.cpp b/projects/hip/src/Cuda2Hip.cpp index 1c4b812506..07529c8e2f 100644 --- a/projects/hip/src/Cuda2Hip.cpp +++ b/projects/hip/src/Cuda2Hip.cpp @@ -221,6 +221,31 @@ namespace { DenseMap cuda2hipRename; }; + StringRef unquoteStr(StringRef s) { + if (s.size() > 1 && s.front() == '"' && s.back() == '"') + return s.substr(1, s.size()-2); + return s; + } + + void processString(StringRef s, struct hipName & map, + Replacements * Replace, SourceManager & SM, SourceLocation start) + { + size_t begin = 0; + while ((begin = s.find("cuda", begin)) != StringRef::npos) { + const size_t end = s.find_first_of(" ", begin + 4); + StringRef name = s.slice(begin, end); + StringRef repName = map.cuda2hipRename[name]; + if (!repName.empty()) { + SourceLocation sl = start.getLocWithOffset(begin + 1); + Replacement Rep(SM, sl, name.size(), repName); + Replace->insert(Rep); + } + if (end == StringRef::npos) break; + begin = end + 1; + } + } + + struct HipifyPPCallbacks : public PPCallbacks, public SourceFileCallbacks { HipifyPPCallbacks(Replacements * R) : SeenEnd(false), _sm(nullptr), _pp(nullptr), Replace(R) @@ -335,22 +360,7 @@ namespace { if (tok.is(tok::string_literal)) { StringRef s(tok.getLiteralData(), tok.getLength()); - size_t begin = 0; - while ((begin = s.find("cuda", begin)) != StringRef::npos) { - const size_t end = s.find_first_of(" ", begin + 4); - StringRef name = s.slice(begin, end); - llvm::outs() << "\nToken: <" << name << "> found in string literal '" - << s << "' as argument in expansion of macro '" << macroName << "'\n"; - StringRef repName = N.cuda2hipRename[name]; - if (!repName.empty()) { - llvm::outs() << "\nWill be replaced with: <" << repName << "\n"; - SourceLocation sl = tok.getLocation().getLocWithOffset(begin); - Replacement Rep(*_sm, sl, name.size(), repName); - Replace->insert(Rep); - } - if (end == StringRef::npos) break; - begin = end + 1; - } + processString(unquoteStr(s), N, Replace, *_sm, tok.getLocation()); } } } @@ -560,23 +570,7 @@ class Cuda2HipCallback : public MatchFinder::MatchCallback { if (const StringLiteral * stringLiteral = Result.Nodes.getNodeAs("stringLiteral")) { StringRef s = stringLiteral->getString(); - size_t begin = 0; - while ((begin = s.find("cuda", begin)) != StringRef::npos) { - const size_t end = s.find_first_of(" ", begin + 4); - StringRef name = s.slice(begin, end); - llvm::outs() << "\nToken: <" << name << "> found in string literal " << s << "\n"; - StringRef repName = N.cuda2hipRename[name]; - if (!repName.empty()) { - llvm::outs() << "\nWill be replaced with: <" << repName << "\n"; - SourceLocation sl = stringLiteral->getLocationOfByte(begin, *SM, - Result.Context->getLangOpts(), Result.Context->getTargetInfo()); - Replacement Rep(*SM, SM->isMacroArgExpansion(sl) ? - SM->getImmediateSpellingLoc(sl) : sl, name.size(), repName); - Replace->insert(Rep); - } - if (end == StringRef::npos) break; - begin = end + 1; - } + processString(s, N, Replace, *SM, stringLiteral->getLocStart()); } if (const UnaryExprOrTypeTraitExpr * expr = Result.Nodes.getNodeAs("cudaStructSizeOf"))