Skip to content

Commit

Permalink
Added test cases for rule file inclusion with ~include
Browse files Browse the repository at this point in the history
  • Loading branch information
TejaX-Alaghari committed Feb 14, 2025
1 parent 800fb88 commit 8a3c6cd
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 12 deletions.
32 changes: 20 additions & 12 deletions clang/lib/DPCT/UserDefinedRules/UserDefinedRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ readYAMLFile(const llvm::StringRef &ruleFilePath) {
dpctExit(MigrationErrorInvalidRuleFilePath);
}

// Get the directory path of the rule file.
llvm::SmallString<128> directoryPath(ruleFilePath);
llvm::sys::path::remove_filename(directoryPath);

Expand All @@ -370,21 +371,28 @@ readYAMLFile(const llvm::StringRef &ruleFilePath) {
for (auto line : lines) {
auto lineStr = line.str();

// Check if this line starts with "!include"
if (lineStr.compare(0, 8, "!include") == 0) {
// Extract the filename following !include.
std::istringstream iss(lineStr);
std::string incDirective, incRuleFilePath;
iss >> incDirective >> incRuleFilePath;

if (incDirective == "!include" && !incRuleFilePath.empty()) {
// Recursively process the included file.
llvm::SmallString<128> incRuleFileAbsPath = directoryPath;
llvm::sys::path::append(incRuleFileAbsPath, incRuleFilePath);
// Extract the filename following !include.
std::istringstream iss(lineStr);
std::string incDirective, incRuleFilePath;
iss >> incDirective >> incRuleFilePath;

if (incDirective == "!include" && !incRuleFilePath.empty()) {
// Remove surrounding quotes if they exist.
if (incRuleFilePath.front() == '"' && incRuleFilePath.back() == '"') {
incRuleFilePath = incRuleFilePath.substr(1, incRuleFilePath.size() - 2);
}

// Calculate the absolute path of the included file based on its parent
// rule file
llvm::SmallString<128> incRuleFileAbsPath = directoryPath;
llvm::sys::path::append(incRuleFileAbsPath, incRuleFilePath);

// Recursively process the included file.
if (llvm::sys::fs::exists(incRuleFileAbsPath)) {
output << readYAMLFile(incRuleFileAbsPath.str())->getBuffer().str()
<< "\n";
} else {
output << lineStr << "\n";
output << readYAMLFile(incRuleFilePath)->getBuffer().str() << "\n";
}
} else {
output << lineStr << "\n";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import a_repl
import b_repl

a_repl()
b_repl()
b1_repl()
b2_repl()
b3_repl()
b4_repl()
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: rm -rf %T && mkdir -p %T
// RUN: cd %T
// RUN: cp %S/input.py ./
// RUN: cp %S/expected.py ./
// RUN: cp -r %S/rules ./
// RUN: echo "!include %T/rules/b/b4.yaml" >> %T/rules/python_build_script_migration_rule_inc.yaml

// RUN: dpct -in-root ./ --migrate-build-script-only --migrate-build-script=Python --rule-file=%T/rules/python_build_script_migration_rule_inc.yaml
// RUN: echo "begin" > %T/diff.txt
// RUN: diff --strip-trailing-cr %S/expected.py %T/dpct_output/input.py >> %T/diff.txt
// RUN: echo "end" >> %T/diff.txt
// CHECK: begin
// CHECK-NEXT: end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import a
import b

a()
b()
b1()
b2()
b3()
b4()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- Rule: rule_call_a
Kind: PythonRule
Priority: Fallback
MatchMode: Partial
PythonSyntax: call_a
In: a()
Out: a_repl()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- Rule: rule_call_b
Kind: PythonRule
Priority: Fallback
MatchMode: Partial
PythonSyntax: call_b
In: b()
Out: b_repl()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- Rule: rule_call_b1
Kind: PythonRule
Priority: Fallback
MatchMode: Partial
PythonSyntax: call_b1
In: b1()
Out: b1_repl()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- Rule: rule_call_b2
Kind: PythonRule
Priority: Fallback
MatchMode: Partial
PythonSyntax: call_b2
In: b2()
Out: b2_repl()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- Rule: rule_call_b3
Kind: PythonRule
Priority: Fallback
MatchMode: Partial
PythonSyntax: call_b3
In: b3()
Out: b3_repl()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- Rule: rule_call_b4
Kind: PythonRule
Priority: Fallback
MatchMode: Partial
PythonSyntax: call_b4
In: b4()
Out: b4_repl()
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
!include a.yaml

- Rule: rule_import_a
Kind: PythonRule
Priority: Fallback
MatchMode: Partial
PythonSyntax: import_a
In: import a
Out: import a_repl

!include b/b.yaml

- Rule: rule_import_b
Kind: PythonRule
Priority: Fallback
MatchMode: Partial
PythonSyntax: import_b
In: import b
Out: import b_repl

!include "b/b1.yaml"

!include b/b2.yaml

!include b/b3.yaml

0 comments on commit 8a3c6cd

Please sign in to comment.