Skip to content

write double backslashes in paths in #line directives #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

mariusbancila
Copy link

When the #line directives are emitted, the path of the compiled file is written in the output cpp file. However, double backslashes are not used which may lead to interpreting characters as escape sequences when a relative path is supplied as argument.

Consider this source code located in a file called demo.cpp2 in a folder called learn:

main: () -> int = {
   std::cout << "Hello, world!\n";
}

Compile it with a relative path:

cppfront.exe ..\learn\demo.cpp2

It produces this output:

// ----- Cpp2 support -----
#include "cpp2util.h"


#line 1 "..\learn\demo.cpp2"
[[nodiscard]] auto main() -> int;

//=== Cpp2 definitions ==========================================================

#line 1 "..\learn\demo.cpp2"
[[nodiscard]] auto main() -> int{
   std::cout << "Hello, world!\n";
}

When you run this with cl.exe you get the following warnings:

demo.cpp
..\learn\demo.cpp(5): warning C4129: 'l': unrecognized character escape sequence
..\learn\demo.cpp(5): warning C4129: 'd': unrecognized character escape sequence
..learndemo.cpp2(5): warning C4129: 'l': unrecognized character escape sequence
..learndemo.cpp2(5): warning C4129: 'd': unrecognized character escape sequence

This PR fixes this so that the output is as follows:

// ----- Cpp2 support -----
#include "cpp2util.h"


#line 1 "..\\learn\\demo.cpp2"
[[nodiscard]] auto main() -> int;

//=== Cpp2 definitions ==========================================================

#line 1 "..\\learn\\demo.cpp2"
[[nodiscard]] auto main() -> int{
   std::cout << "Hello, world!\n";
}

@MichaelCook
Copy link

Could also just stop using backslashes entirely.
At the earliest opportunity, convert any backslashes in pathnames to forward slashes.
In Windows, they're equivalent (though some Windows applications treat forward slashes on the command line specially).

@JohelEGP
Copy link
Contributor

That'd mean using std::filesystem::path in the generic format instead of std::string, right?

Co-authored-by: Johel Ernesto Guerrero Peña <[email protected]>
@hsutter
Copy link
Owner

hsutter commented Sep 28, 2022

Thanks, good point -- I'll add a std::quoted there in an upcoming checkin separately that will close this one.

@hsutter hsutter self-assigned this Sep 28, 2022
@hsutter hsutter closed this in 634a73a Oct 1, 2022
@mariusbancila mariusbancila deleted the paths-in-line-directive branch October 20, 2022 10:45
Azmah-Bad pushed a commit to Azmah-Bad/cppfront that referenced this pull request Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants