Skip to content

Commit

Permalink
Reformat C++ code
Browse files Browse the repository at this point in the history
  • Loading branch information
hlopko committed May 27, 2021
1 parent 3cef53c commit 74c2c96
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
31 changes: 17 additions & 14 deletions util/launcher/launcher_main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{BufReader, BufRead};
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::process::Command;
use std::vec::Vec;
Expand All @@ -11,7 +11,7 @@ use std::os::unix::process::CommandExt;
/// This string must match the one found in `_create_test_launcher`
const LAUNCHFILES_ENV_PATH: &'static str = ".launchfiles/env";

/// Load environment variables from a uniquly formatted
/// Load environment variables from a uniquly formatted
fn environ() -> BTreeMap<String, String> {
let mut environ = BTreeMap::new();

Expand All @@ -21,7 +21,7 @@ fn environ() -> BTreeMap<String, String> {
let env_path = std::env::args().nth(0).expect("arg 0 was not set") + LAUNCHFILES_ENV_PATH;
let file = File::open(env_path).expect("Failed to load the environment file");

// Variables will have the `${pwd}` variable replaced which is rendered by
// Variables will have the `${pwd}` variable replaced which is rendered by
// `@rules_rust//rust/private:util.bzl::expand_locations`
let pwd = std::env::current_dir().expect("Failed to get current working directory");
let pwd_str = pwd.to_string_lossy();
Expand All @@ -35,25 +35,28 @@ fn environ() -> BTreeMap<String, String> {

environ.insert(
key.expect("Key is not set"),
line.expect("Failed to read line").replace("${pwd}", &pwd_str),
line.expect("Failed to read line")
.replace("${pwd}", &pwd_str),
);

key = None;
}

environ
}

/// Locate the executable based on the name of the launcher executable
fn executable() -> PathBuf {
let mut exec_path = std::env::args().nth(0).expect("arg 0 was not set");
let stem_index = exec_path.rfind(".launcher").expect("This executable should always contain `.launcher`");
let stem_index = exec_path
.rfind(".launcher")
.expect("This executable should always contain `.launcher`");

// Remove the substring from the exec path
for _char in ".launcher".chars() {
exec_path.remove(stem_index);
}

PathBuf::from(exec_path)
}

Expand All @@ -67,9 +70,9 @@ fn args() -> Vec<String> {
#[cfg(target_family = "unix")]
fn exec(environ: BTreeMap<String, String>, executable: PathBuf, args: Vec<String>) {
let error = Command::new(&executable)
.envs(environ.iter())
.args(args)
.exec();
.envs(environ.iter())
.args(args)
.exec();

panic!("Process failed to start: {:?} with {:?}", executable, error)
}
Expand All @@ -79,10 +82,10 @@ fn exec(environ: BTreeMap<String, String>, executable: PathBuf, args: Vec<String
#[cfg(target_family = "windows")]
fn exec(environ: BTreeMap<String, String>, executable: PathBuf, args: Vec<String>) {
let output = Command::new(executable)
.envs(environ.iter())
.args(args)
.output()
.expect("Failed to run process");
.envs(environ.iter())
.args(args)
.output()
.expect("Failed to run process");

std::process::exit(output.status.code().unwrap_or(1));
}
Expand Down
3 changes: 2 additions & 1 deletion util/process_wrapper/process_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ int PW_MAIN(int argc, const CharType* argv[], const CharType* envp[]) {
}

// Have the last values added take precedence over the first.
// This is simpler than needing to track duplicates and explicitly override them.
// This is simpler than needing to track duplicates and explicitly override
// them.
std::reverse(environment_block.begin(), environment_block.end());

int exit_code = System::Exec(exec_path, arguments, environment_block,
Expand Down
2 changes: 1 addition & 1 deletion util/process_wrapper/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class System {
// It is meant to be called once during the lifetime of the parent process
static int Exec(const StrType& executable, const Arguments& arguments,
const EnvironmentBlock& environment_block,
const StrType& stdout_file,const StrType& stderr_file);
const StrType& stdout_file, const StrType& stderr_file);
};

} // namespace process_wrapper
Expand Down
6 changes: 3 additions & 3 deletions util/process_wrapper/system_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace process_wrapper {
namespace {

class OutputPipe {
public:
public:
static constexpr size_t kReadEndDesc = 0;
static constexpr size_t kWriteEndDesc = 1;

Expand Down Expand Up @@ -97,7 +97,7 @@ class OutputPipe {
return true;
}

private:
private:
void Close(size_t idx) {
if (output_pipe_desc_[idx] > 0) {
close(output_pipe_desc_[idx]);
Expand All @@ -107,7 +107,7 @@ class OutputPipe {
int output_pipe_desc_[2] = {-1};
};

} // namespace
} // namespace

System::StrType System::GetWorkingDirectory() {
const size_t kMaxBufferLength = 4096;
Expand Down
10 changes: 5 additions & 5 deletions util/process_wrapper/system_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ std::string GetLastErrorAsStr() {
}

class OutputPipe {
public:
public:
static constexpr size_t kReadEndHandle = 0;
static constexpr size_t kWriteEndHandle = 1;

Expand Down Expand Up @@ -181,7 +181,7 @@ class OutputPipe {
return true;
}

private:
private:
void Close(size_t idx) {
if (output_pipe_handles_[idx] != nullptr) {
::CloseHandle(output_pipe_handles_[idx]);
Expand All @@ -191,7 +191,7 @@ class OutputPipe {
HANDLE output_pipe_handles_[2] = {nullptr};
};

} // namespace
} // namespace

System::StrType System::GetWorkingDirectory() {
constexpr DWORD kMaxBufferLength = 4096;
Expand Down Expand Up @@ -243,7 +243,7 @@ int System::Exec(const System::StrType& executable,
/*dwCreationFlags*/ 0
#if defined(UNICODE)
| CREATE_UNICODE_ENVIRONMENT
#endif // defined(UNICODE)
#endif // defined(UNICODE)
,
/*lpEnvironment*/ environment_block_win.empty()
? nullptr
Expand Down Expand Up @@ -277,4 +277,4 @@ int System::Exec(const System::StrType& executable,
return exit_status;
}

} // namespace process_wrapper
} // namespace process_wrapper
5 changes: 3 additions & 2 deletions util/process_wrapper/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ bool ReadFileToArray(const System::StrType& file_path,
}

// a \ at the end of a line allows us to escape the new line break,
// \\ yields a single \, so \\\ translates to a single \ and a new line escape
// \\ yields a single \, so \\\ translates to a single \ and a new line
// escape
int end_backslash_count = 0;
for (std::string::reverse_iterator rit = read_line.rbegin();
rit != read_line.rend() && *rit == '\\'; ++rit) {
Expand Down Expand Up @@ -98,6 +99,6 @@ bool ReadFileToArray(const System::StrType& file_path,
}
}
return true;
}
}

} // namespace process_wrapper

0 comments on commit 74c2c96

Please sign in to comment.