diff --git a/src/cli.rs b/src/cli.rs index 1c75ec3..e4aa613 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -179,6 +179,12 @@ pub struct Opts { /// original files. #[clap(long)] pub line_directives: bool, + + /// Add #pragma once + /// + /// This will prepend #pragma once to the amalgamated file + #[clap(long)] + pub pragma_once: bool, } fn with_indices<'a, T>( diff --git a/src/main.rs b/src/main.rs index 54f4df9..5a0bd9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,12 +71,14 @@ fn run_with_writer(opts: &Opts, writer: impl Write) -> Result<()> { unresolvable_quote_include: opts.unresolvable_quote_include_handling(), unresolvable_system_include: opts.unresolvable_system_include_handling(), }; + let pragma_once = opts.pragma_once; let mut processor = Processor::new( writer, resolver, opts.line_directives, filter, error_handling_opts, + pragma_once, ); opts.files .iter() diff --git a/src/process.rs b/src/process.rs index 556ac41..ece2439 100644 --- a/src/process.rs +++ b/src/process.rs @@ -98,6 +98,8 @@ pub struct Processor { expected_line: Option, error_handling_opts: ErrorHandlingOpts, regexes: Regexes, + guard_emitted: bool, + pragma_once: bool, } impl Processor { @@ -107,6 +109,7 @@ impl Processor { line_directives: bool, inlining_filter: InliningFilter, error_handling_opts: ErrorHandlingOpts, + pragma_once: bool, ) -> Self { let expected_line = line_directives.then(|| LineRef { file_idx: EMPTY_STACK_IDX, @@ -122,6 +125,8 @@ impl Processor { expected_line, error_handling_opts, regexes: Regexes::new(), + guard_emitted: false, + pragma_once, } } @@ -306,6 +311,10 @@ impl Processor { } pub fn process(&mut self, source_file: &Path) -> Result<()> { + if self.pragma_once && !self.guard_emitted { + self.guard_emitted = true; + self.writer.write(b"#pragma once\n")?; + } info!("Processing source file {:?}", debug_file_name(source_file)); let canonical_path = source_file.canonicalize().with_context(|| { format!( diff --git a/tests/integration/misc.rs b/tests/integration/misc.rs index 2015345..629d92d 100644 --- a/tests/integration/misc.rs +++ b/tests/integration/misc.rs @@ -53,3 +53,15 @@ fn multiple_source_files() -> Result<()> { .stdout("abc"); Ok(()) } + +#[test] +fn pragma_once() -> Result<()> { + util::builder() + .source_file("test")? + .command() + .arg("--pragma-once") + .assert() + .success() + .stdout("#pragma once\ntest"); + Ok(()) +}