Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ pub struct Processor<W> {
expected_line: Option<LineRef>,
error_handling_opts: ErrorHandlingOpts,
regexes: Regexes,
guard_emitted: bool,
pragma_once: bool,
}

impl<W: Write> Processor<W> {
Expand All @@ -107,6 +109,7 @@ impl<W: Write> Processor<W> {
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,
Expand All @@ -122,6 +125,8 @@ impl<W: Write> Processor<W> {
expected_line,
error_handling_opts,
regexes: Regexes::new(),
guard_emitted: false,
pragma_once,
}
}

Expand Down Expand Up @@ -306,6 +311,10 @@ impl<W: Write> Processor<W> {
}

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!(
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}