Skip to content
This repository was archived by the owner on Feb 24, 2024. It is now read-only.

Commit bd4d4db

Browse files
committed
Fix error when parsing files with circular dependencies
Fix doesn't guarantee that compilation process will be successful if output file was generated from source files with circular dependencies. This is not in scope of this project. Circular dependencies shouldn't occur in well organized projects. This fix has minor side effect. Order of source files in generated output file can slightly change.
1 parent 29bc54a commit bd4d4db

File tree

7 files changed

+43
-6
lines changed

7 files changed

+43
-6
lines changed

src/parse/CppFileMerger.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ export default class CppFileMerger {
4949
const fileContent = readFile(filePath);
5050
const result = this.parser.parse(fileContent);
5151
result.systemIncludes.forEach(include => this.systemIncludes.add(include));
52-
const currentDirectory = path.dirname(filePath);
53-
const localIncludesContent = result.localIncludes.map(includeFilePath => {
54-
return this.parseIncludedFile(includeFilePath, currentDirectory, filePath)
55-
});
5652

5753
if (result.processOnce) {
5854
this.processedOnce.add(filePath);
5955
}
6056

57+
const currentDirectory = path.dirname(filePath);
58+
const localIncludesContent = result.localIncludes.map(includeFilePath => {
59+
return this.parseIncludedFile(includeFilePath, currentDirectory, filePath)
60+
});
61+
6162
return [
6263
...localIncludesContent,
6364
result.content
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
3+
// second.hpp
4+
5+
// first.hpp
6+
7+
// main.cpp
8+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include "second.hpp"
4+
5+
#include <iostream>
6+
7+
// first.hpp
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "first.hpp"
2+
3+
#include <iostream>
4+
5+
// main.cpp
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
#include "first.hpp"
4+
5+
// second.hpp

test/data/complex/expected.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
// main.cpp
1414

15-
// zero.c
16-
1715
// first.cpp
1816

17+
// zero.c
18+
1919
// second.cpp

test/unit/parse/CppFileMerger.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,14 @@ describe("Merging source files in subdirectories", () => {
3838
expect(content).toEqual(expected);
3939
});
4040
});
41+
42+
describe("Merging files with circular dependencies", () => {
43+
const dataDirectory = "test/data/circularDependencies";
44+
const merger = new CppFileMerger();
45+
const content = merger.parse(`${dataDirectory}/main.cpp`);
46+
47+
test("Generated content equals expected", () => {
48+
const expected = fs.readFileSync(`${dataDirectory}/expected.cpp`, "utf-8");
49+
expect(content).toEqual(expected);
50+
});
51+
});

0 commit comments

Comments
 (0)