compiler: Track/Report Relative Paths Names
Continuous Integration / Grammar Tests (push) Successful in 37s
Continuous Integration / Library Format Tests (push) Successful in 1m22s
Continuous Integration / Library Tests (push) Successful in 4m23s

When the user gives relative path names for p4 files, report
those in error messages (etc.). The SourceManager can/does
resolve those to absolute path names.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-22 04:27:37 -04:00
parent 0cb50e2e2f
commit 041009a22e
3 changed files with 33 additions and 59 deletions
+16 -32
View File
@@ -85,7 +85,7 @@ public struct SourceManager {
let fm = FileManager()
for path in self.paths {
let combined = path.pushing(file)
let combined = path.pushing(file).lexicallyNormalized()
if fm.fileExists(atPath: combined.string) {
return combined
}
@@ -247,14 +247,24 @@ public struct SourceCode {
}
func do_preprocess(
_ file: URL, _ manager: SourceManager, _ starting: Int = 0
_ file: FilePath, _ manager: SourceManager, _ starting: Int = 0
) -> Result<(FileSourceLocation, String)> {
// First (1) match group has the name of the include file.
let re = /\#include[\s]*<([^\s>]*)>/
do {
var locations: [FileSourceLocation] = Array()
var contents = try String.init(contentsOf: file, encoding: String.defaultCStringEncoding)
// First, try to find the file.
guard let included_path = manager.firstExisting(file) else {
return .Error(
Error(
withMessage:
"Could not open \(file) for preprocessing"))
}
var contents = try String.init(
contentsOf: URL(filePath: included_path.string), encoding: String.defaultCStringEncoding)
var changed = true
while changed {
@@ -264,20 +274,9 @@ func do_preprocess(
let before = contents[..<match.range.lowerBound]
let after = contents[match.range.upperBound...]
// Determine whether there is a file with that name in the include path.
guard let included_path = manager.firstExisting(FilePath.init("\(match.1)")) else {
return .Error(
Error(
withMessage:
"Could not open \(match.1) for inclusion"))
}
// Try to make a url from the configured file.
let included_url = URL.init(filePath: included_path.string)
// By calling ourselves recursively, the include being processed will
// be _completely_ expanded (including any nested includes).
switch do_preprocess(included_url, manager, starting + before.count) {
switch do_preprocess(FilePath("\(match.1)"), manager, starting + before.count) {
case .Ok((let location, let expanded)):
// Recombine what was before and after the include being processed
// with the expanded text.
@@ -298,7 +297,7 @@ func do_preprocess(
return .Ok(
(
FileSourceLocation(
SourceLocation(starting..<(starting + contents.count)), FilePath(file.path()), locations),
SourceLocation(starting..<(starting + contents.count)), file, locations),
contents
))
@@ -316,22 +315,7 @@ public struct SourceCodePreprocessor {
}
public func preprocess(_ file: FilePath) -> Result<SourceCode> {
// First, decide whether the given file exists at the path the user gave.
let fm = FileManager()
let file_to_open =
if !fm.fileExists(atPath: file.string) {
self.manager.firstExisting(file)
} else {
file
}
guard let file_to_open else {
return .Error(Error(withMessage: "Could not open \(file) for preprocessing"))
}
let url = URL.init(filePath: file_to_open.string)
switch do_preprocess(url, self.manager) {
switch do_preprocess(file, self.manager) {
case .Ok((let location, let contents)):
return .Ok(SourceCode(contents, self.manager, location))
case .Error(let e): return .Error(e)