C++: add #pragma once to generated file

This commit is contained in:
Olivier Goffart 2024-04-22 12:43:36 +02:00
parent 475ced0a62
commit 5a4713aa17
3 changed files with 10 additions and 0 deletions

View file

@ -3,6 +3,8 @@
#include "logic.h"
#include "appwindow.h"
// Test that it's ok to include twice
#include "appwindow.h"
void setup_logic(const Logic &logic)
{

View file

@ -89,6 +89,8 @@ mod cpp_ast {
impl Display for File {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
writeln!(f, "// This file is auto-generated")?;
writeln!(f, "#pragma once")?;
for i in &self.includes {
writeln!(f, "#include {}", i)?;
}

View file

@ -43,6 +43,12 @@ pub fn test(testcase: &test_driver_lib::TestCase) -> Result<(), Box<dyn Error>>
return Err(vec.join("\n").into());
}
// Remove the `#pragma once` as this is not going to be included and would produce a warning
// when compiling the generated code.
let hash_pos = generated_cpp.iter().position(|&b| b == b'#').unwrap();
assert_eq!(&generated_cpp[hash_pos..hash_pos + 12], b"#pragma once");
generated_cpp.drain(hash_pos..hash_pos + 12);
generated_cpp.write_all(
br"
#ifdef NDEBUG