Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/lib/DPCT/CallExprRewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,7 @@ class UserDefinedRewriterFactory : public CallExprRewriterFactoryBase {
Priority = R.Priority;
OB.Kind = OutputBuilder::Kind::Top;
OB.RuleName = R.RuleId;
OB.RuleFile = R.RuleFile;
OB.parse(OutStr);
}

Expand Down
134 changes: 51 additions & 83 deletions clang/lib/DPCT/Rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void importRules(llvm::cl::list<std::string> &RuleFiles) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
llvm::MemoryBuffer::getFile(RuleFile);
if (!Buffer) {
llvm::errs() << "error: failed to read " << RuleFile << ": "
llvm::errs() << "Error: failed to read " << RuleFile << ": "
<< Buffer.getError().message() << "\n";
clang::dpct::ShowStatus(MigrationErrorInvalidRuleFilePath);
dpctExit(MigrationErrorInvalidRuleFilePath);
Expand All @@ -228,6 +228,7 @@ void importRules(llvm::cl::list<std::string> &RuleFiles) {

// Register Rules
for (std::shared_ptr<MetaRuleObject> &r : CurrentRules) {
r->RuleFile = RuleFile;
switch (r->Kind) {
case (RuleKind::Macro):
registerMacroRule(*r);
Expand Down Expand Up @@ -304,106 +305,73 @@ void OutputBuilder::ignoreWhitespaces(std::string &OutStr, size_t &Idx) {
}

// /OutStr is the string specified in rule's "Out" session
void OutputBuilder::consumeRParen(std::string &OutStr, size_t &Idx) {
void OutputBuilder::consumeRParen(std::string &OutStr, size_t &Idx,
std::string &&Keyword) {
ignoreWhitespaces(OutStr, Idx);
if (Idx >= OutStr.size()) {
llvm::errs() << "rule parse error: in rule " << RuleName
<< ", expect an ')' at end of 'Out' option value.\n";
if (Idx >= OutStr.size() || OutStr[Idx] != ')') {
llvm::errs() << RuleFile << ":Error: in rule " << RuleName
<< ", ')' is expected after " << Keyword << "\n";
clang::dpct::ShowStatus(MigrationErrorCannotParseRuleFile);
dpctExit(MigrationErrorCannotParseRuleFile);
}

if (OutStr[Idx] != ')') {
llvm::errs() << "rule parse error : in rule " << RuleName
<< ", expect an ')' in 'Out' option value around: "
<< OutStr.substr(Idx, 10) << "\n";
clang::dpct::ShowStatus(MigrationErrorCannotParseRuleFile);
dpctExit(MigrationErrorCannotParseRuleFile);
} else {
Idx++;
}
Idx++;
}

// /OutStr is the string specified in rule's "Out" session
void OutputBuilder::consumeLParen(std::string &OutStr, size_t &Idx) {
void OutputBuilder::consumeLParen(std::string &OutStr, size_t &Idx,
std::string &&Keyword) {
ignoreWhitespaces(OutStr, Idx);
if (Idx >= OutStr.size()) {
llvm::errs() << "rule parse error: in rule " << RuleName
<< ", expect an '(' at end of 'Out' option value.\n";
if (Idx >= OutStr.size() || OutStr[Idx] != '(') {
llvm::errs() << RuleFile << ":Error: in rule " << RuleName
<< ", '(' is expected after " << Keyword << "\n";
clang::dpct::ShowStatus(MigrationErrorCannotParseRuleFile);
dpctExit(MigrationErrorCannotParseRuleFile);
}

if (OutStr[Idx] != '(') {
llvm::errs() << "rule parse error : in rule " << RuleName
<< ", expect an '(' in 'Out' option value around: "
<< OutStr.substr(Idx, 10) << "\n";
clang::dpct::ShowStatus(MigrationErrorCannotParseRuleFile);
dpctExit(MigrationErrorCannotParseRuleFile);
} else {
Idx++;
}
Idx++;
}

// /OutStr is the string specified in rule's "Out" session
int OutputBuilder::consumeArgIndex(std::string &OutStr, size_t &Idx) {
int OutputBuilder::consumeArgIndex(std::string &OutStr, size_t &Idx,
std::string &&Keyword) {
ignoreWhitespaces(OutStr, Idx);

if (Idx >= OutStr.size()) {
llvm::errs() << "rule parse error: in rule " << RuleName
<< ", expect \'$\' followed by a positive integer at end of "
"'Out' option value.\n";
if (Idx >= OutStr.size() || OutStr[Idx] != '$') {
llvm::errs() << RuleFile << ":Error: in rule " << RuleName
<< ", $ followed by a positive integer is expected after "
<< Keyword << "\n";
clang::dpct::ShowStatus(MigrationErrorCannotParseRuleFile);
dpctExit(MigrationErrorCannotParseRuleFile);
}

if (OutStr[Idx] != '$') {
llvm::errs() << "rule parse error: in rule " << RuleName
<< ", expect \'$\' followed by a positive integer in 'Out' "
"option value around: "
<< OutStr.substr(Idx, 10) << "\n";
clang::dpct::ShowStatus(MigrationErrorCannotParseRuleFile);
dpctExit(MigrationErrorCannotParseRuleFile);
}
// consume $
Idx++;
auto DollarSignIdx = Idx;
ignoreWhitespaces(OutStr, Idx);
int ArgIndex = 0;

if (Idx >= OutStr.size()) {
llvm::errs() << "rule parse error: in rule " << RuleName
<< ", expect a positive integer at end of "
"'Out' option value.\n";
// process arg number
std::string ArgNumStr = OutStr.substr(Idx);
std::size_t pos = 0;

try{
ArgIndex = std::stoi(ArgNumStr, &pos);
} catch(std::invalid_argument const& Ex){
llvm::errs() << RuleFile << ":Error: in rule " << RuleName
<< ", unknown keyword: $" << ArgNumStr.substr(0, 10) << "\n";
clang::dpct::ShowStatus(MigrationErrorCannotParseRuleFile);
dpctExit(MigrationErrorCannotParseRuleFile);
} catch(std::out_of_range const& Ex) {
llvm::errs() << RuleFile << ":Error: in rule " << RuleName
<< ", argument index out of range.\n";
clang::dpct::ShowStatus(MigrationErrorCannotParseRuleFile);
dpctExit(MigrationErrorCannotParseRuleFile);
}
unsigned i = Idx;
for (; i < OutStr.size(); i++) {
if (!std::isdigit(OutStr[i])) {
if (i == Idx) {
// report unknown KW
llvm::errs() << "rule parse error: in rule " << RuleName
<< ", unknown keyword in 'Out' option value around: "
<< OutStr.substr(i, 10) << "\n";
clang::dpct::ShowStatus(MigrationErrorCannotParseRuleFile);
dpctExit(MigrationErrorCannotParseRuleFile);
} else {
break;
}
}
}

// process arg number
std::string ArgNumStr = OutStr.substr(Idx, i - Idx);
Idx = i;
ArgIndex = std::stoi(ArgNumStr);
Idx = Idx + pos;

if (ArgIndex <= 0) {
// report invalid ArgIndex
llvm::errs() << "rule parse error: in rule " << RuleName
<< ", expect a positive integer in 'Out' option value around: "
<< OutStr.substr(DollarSignIdx, 10) << "\n";
llvm::errs() << RuleFile << ":Error: in rule " << RuleName
<< ", expect a positive integer, found " << ArgIndex
<< " after " << Keyword << "\n";
clang::dpct::ShowStatus(MigrationErrorCannotParseRuleFile);
dpctExit(MigrationErrorCannotParseRuleFile);
}
Expand All @@ -427,31 +395,31 @@ OutputBuilder::consumeKeyword(std::string &OutStr, size_t &Idx) {
ResultBuilder->Kind = Kind::Device;
} else if (OutStr.substr(Idx, 13) == "$type_name_of") {
Idx += 13;
consumeLParen(OutStr, Idx);
consumeLParen(OutStr, Idx, "$type_name_of");
ResultBuilder->Kind = Kind::TypeName;
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx);
consumeRParen(OutStr, Idx);
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx, "$type_name_of");
consumeRParen(OutStr, Idx, "$type_name_of");
} else if (OutStr.substr(Idx, 8) == "$addr_of") {
Idx += 8;
consumeLParen(OutStr, Idx);
consumeLParen(OutStr, Idx, "$addr_of");
ResultBuilder->Kind = Kind::AddrOf;
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx);
consumeRParen(OutStr, Idx);
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx, "$addr_of");
consumeRParen(OutStr, Idx, "$addr_of");
} else if (OutStr.substr(Idx, 11) == "$deref_type") {
Idx += 11;
consumeLParen(OutStr, Idx);
consumeLParen(OutStr, Idx, "$deref_type");
ResultBuilder->Kind = Kind::DerefedTypeName;
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx);
consumeRParen(OutStr, Idx);
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx, "$deref_type");
consumeRParen(OutStr, Idx, "$deref_type");
} else if (OutStr.substr(Idx, 6) == "$deref") {
Idx += 6;
consumeLParen(OutStr, Idx);
consumeLParen(OutStr, Idx, "$deref");
ResultBuilder->Kind = Kind::Deref;
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx);
consumeRParen(OutStr, Idx);
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx, "$deref");
consumeRParen(OutStr, Idx, "$deref");
} else {
ResultBuilder->Kind = Kind::Arg;
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx);
ResultBuilder->ArgIndex = consumeArgIndex(OutStr, Idx, "$");
}
return ResultBuilder;
}
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/DPCT/Rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class MetaRuleObject {
ClassMethod() {}
};
static std::vector<std::string> RuleFiles;
std::string RuleFile;
std::string RuleId;
RulePriority Priority;
RuleKind Kind;
Expand Down Expand Up @@ -216,6 +217,7 @@ class OutputBuilder {
DerefedTypeName
};
std::string RuleName;
std::string RuleFile;
Kind Kind;
size_t ArgIndex;
std::string Str;
Expand All @@ -226,10 +228,10 @@ class OutputBuilder {
// /OutStr is the string specified in rule's "Out" session
std::shared_ptr<OutputBuilder> consumeKeyword(std::string &OutStr,
size_t &Idx);
int consumeArgIndex(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);
void consumeLParen(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);
};

void importRules(llvm::cl::list<std::string> &RuleFiles);
Expand Down