Skip to content

Commit

Permalink
Cleanup: Replace NULL with nullptr
Browse files Browse the repository at this point in the history
At several places, NULL and nullptr were mixed. This commit cleans this up and switches NULL to nullptr where appropriate.

Closes bazelbuild#13168.

PiperOrigin-RevId: 362043567
  • Loading branch information
Vertexwahn authored and larsrc-google committed Jul 30, 2021
1 parent ed0100b commit deb1006
Show file tree
Hide file tree
Showing 56 changed files with 441 additions and 432 deletions.
4 changes: 2 additions & 2 deletions examples/windows/dll/hello_world-load-dll-at-runtime.cpp
Expand Up @@ -12,11 +12,11 @@ int main() {

hellolib = LoadLibrary(TEXT("hellolib.dll"));

if (hellolib != NULL) {
if (hellolib != nullptr) {
get_time = (GET_TIME_PTR)GetProcAddress(hellolib, "get_time");
say_hello = (SAY_HELLO_PTR)GetProcAddress(hellolib, "say_hello");

if (NULL != get_time && NULL != say_hello) {
if (nullptr != get_time && nullptr != say_hello) {
success = TRUE;
char *now = get_time();
say_hello(now);
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/archive_utils.cc
Expand Up @@ -37,7 +37,7 @@ struct PartialZipExtractor : public devtools_ijar::ZipExtractorProcessor {

// Scan the zip file "archive_path" until a file named "stop_entry" is seen,
// then stop.
// If entry_names is not null, it receives a list of all file members
// If entry_names is not nullptr, it receives a list of all file members
// up to and including "stop_entry".
// If a callback is given, it is run with the name and contents of
// each such member.
Expand Down
6 changes: 3 additions & 3 deletions src/main/cpp/blaze.cc
Expand Up @@ -737,7 +737,7 @@ static void WriteFileToStderrOrDie(const blaze_util::Path &path) {
FILE *fp = fopen(path.AsNativePath().c_str(), "r");
#endif

if (fp == NULL) {
if (fp == nullptr) {
BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR)
<< "opening " << path.AsPrintablePath()
<< " failed: " << GetLastErrorString();
Expand Down Expand Up @@ -1383,7 +1383,7 @@ static map<string, EnvVarValue> PrepareEnvironmentForJvm() {
// environment variables to modify the current process, we may actually use
// such map to configure a process from scratch (via interfaces like execvpe
// or posix_spawn), so we need to inherit any untouched variables.
for (char **entry = environ; *entry != NULL; entry++) {
for (char **entry = environ; *entry != nullptr; entry++) {
const std::string var_value = *entry;
std::string::size_type equals = var_value.find('=');
if (equals == std::string::npos) {
Expand Down Expand Up @@ -2085,7 +2085,7 @@ unsigned int BlazeServer::Communicate(

// Execute the requested program, but before doing so, flush everything
// we still have to say.
fflush(NULL);
fflush(nullptr);
ExecuteRunRequest(blaze_util::Path(request.argv(0)), argv);
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/cpp/blaze_util.cc
Expand Up @@ -51,20 +51,20 @@ const char* GetUnaryOption(const char *arg,
const char *next_arg,
const char *key) {
const char *value = blaze_util::var_strprefix(arg, key);
if (value == NULL) {
return NULL;
if (value == nullptr) {
return nullptr;
} else if (value[0] == '=') {
return value + 1;
} else if (value[0]) {
return NULL; // trailing garbage in key name
return nullptr; // trailing garbage in key name
}

return next_arg;
}

bool GetNullaryOption(const char *arg, const char *key) {
const char *value = blaze_util::var_strprefix(arg, key);
if (value == NULL) {
if (value == nullptr) {
return false;
} else if (value[0] == '=') {
BAZEL_DIE(blaze_exit_code::BAD_ARGV)
Expand Down Expand Up @@ -114,7 +114,7 @@ std::vector<std::string> GetAllUnaryOptionValues(
const char* SearchUnaryOption(const vector<string>& args,
const char *key, bool warn_if_dupe) {
if (args.empty()) {
return NULL;
return nullptr;
}

const char* value = nullptr;
Expand All @@ -138,7 +138,7 @@ const char* SearchUnaryOption(const vector<string>& args,
const char* result = GetUnaryOption(args[i].c_str(),
args[i + 1].c_str(),
key);
if (result != NULL) {
if (result != nullptr) {
// 'key' was found and 'result' has its value.
if (value) {
// 'key' was found once before, because 'value' is not empty.
Expand All @@ -158,7 +158,7 @@ const char* SearchUnaryOption(const vector<string>& args,
if (!found_dupe) {
// We did not find a duplicate in the first N-1 arguments. Examine the
// last argument, it may be a duplicate.
found_dupe = (GetUnaryOption(args[i].c_str(), NULL, key) != nullptr);
found_dupe = (GetUnaryOption(args[i].c_str(), nullptr, key) != nullptr);
}
if (found_dupe) {
BAZEL_LOG(WARNING) << key << " is given more than once, "
Expand All @@ -170,7 +170,7 @@ const char* SearchUnaryOption(const vector<string>& args,
// 'value' is empty, so 'key' was not yet found in the first N-1 arguments.
// If 'key' is in the last argument, we'll parse and return the value from
// that, and if it isn't, we'll return NULL.
return GetUnaryOption(args[i].c_str(), NULL, key);
return GetUnaryOption(args[i].c_str(), nullptr, key);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/blaze_util.h
Expand Up @@ -49,7 +49,7 @@ bool GetNullaryOption(const char *arg, const char *key);
// When 'warn_if_dupe' is true, the method checks if 'key' is specified more
// than once and prints a warning if so.
// Returns the value of the 'key' flag iff it occurs in args.
// Returns NULL otherwise.
// Returns nullptr otherwise.
const char* SearchUnaryOption(const std::vector<std::string>& args,
const char* key, bool warn_if_dupe);

Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/blaze_util_bsd.cc
Expand Up @@ -63,10 +63,10 @@ using std::string;
string GetOutputRoot() {
char buf[2048];
struct passwd pwbuf;
struct passwd *pw = NULL;
struct passwd *pw = nullptr;
int uid = getuid();
int r = getpwuid_r(uid, &pwbuf, buf, 2048, &pw);
if (r != -1 && pw != NULL) {
if (r != -1 && pw != nullptr) {
return blaze_util::JoinPath(pw->pw_dir, ".cache/bazel");
} else {
return "/tmp";
Expand Down
16 changes: 8 additions & 8 deletions src/main/cpp/blaze_util_darwin.cc
Expand Up @@ -49,7 +49,7 @@ using std::string;
using std::vector;

// A stack based class for RAII type handling of CF based types that need
// CFRelease called on them. Checks for NULL before calling release.
// CFRelease called on them. Checks for nullptr before calling release.
template <typename T> class CFScopedReleaser {
public:
explicit CFScopedReleaser(T value) : value_(value) { }
Expand All @@ -60,7 +60,7 @@ template <typename T> class CFScopedReleaser {
}
T get() { return value_; }
operator T() { return value_; }
bool isValid() { return value_ != NULL; }
bool isValid() { return value_ != nullptr; }

private:
T value_;
Expand Down Expand Up @@ -105,8 +105,8 @@ void WarnFilesystemType(const blaze_util::Path &output_base) {
kCFAllocatorDefault,
reinterpret_cast<const UInt8 *>(output_base.AsNativePath().c_str()),
output_base.AsNativePath().length(), true));
CFBooleanRef cf_local = NULL;
CFErrorRef cf_error = NULL;
CFBooleanRef cf_local = nullptr;
CFErrorRef cf_error = nullptr;
if (!cf_url.isValid() ||
!CFURLCopyResourcePropertyForKey(cf_url, kCFURLVolumeIsLocalKey,
&cf_local, &cf_error)) {
Expand Down Expand Up @@ -136,7 +136,7 @@ string GetSelfPath(const char* argv0) {

uint64_t GetMillisecondsMonotonic() {
struct timeval ts = {};
if (gettimeofday(&ts, NULL) < 0) {
if (gettimeofday(&ts, nullptr) < 0) {
BAZEL_DIE(blaze_exit_code::INTERNAL_ERROR)
<< "error calling gettimeofday: " << GetLastErrorString();
}
Expand Down Expand Up @@ -178,14 +178,14 @@ string GetSystemJavabase() {

// java_home will print a warning if no JDK could be found
FILE *output = popen("/usr/libexec/java_home -v 1.8+ 2> /dev/null", "r");
if (output == NULL) {
if (output == nullptr) {
return "";
}

char buf[512];
char *result = fgets(buf, sizeof(buf), output);
pclose(output);
if (result == NULL) {
if (result == nullptr) {
return "";
}

Expand Down Expand Up @@ -225,7 +225,7 @@ void ExcludePathFromBackup(const blaze_util::Path &path) {
<< "' from backups";
return;
}
CFErrorRef cf_error = NULL;
CFErrorRef cf_error = nullptr;
if (!CFURLSetResourcePropertyForKey(cf_url, kCFURLIsExcludedFromBackupKey,
kCFBooleanTrue, &cf_error)) {
CFScopedReleaser<CFErrorRef> cf_error_releaser(cf_error);
Expand Down
6 changes: 3 additions & 3 deletions src/main/cpp/blaze_util_linux.cc
Expand Up @@ -51,10 +51,10 @@ string GetOutputRoot() {
} else {
char buf[2048];
struct passwd pwbuf;
struct passwd *pw = NULL;
struct passwd *pw = nullptr;
int uid = getuid();
int r = getpwuid_r(uid, &pwbuf, buf, 2048, &pw);
if (r != -1 && pw != NULL) {
if (r != -1 && pw != nullptr) {
base = pw->pw_dir;
}
}
Expand Down Expand Up @@ -152,7 +152,7 @@ string GetSystemJavabase() {

// Resolve all symlinks.
char resolved_path[PATH_MAX];
if (realpath(javac_dir.c_str(), resolved_path) == NULL) {
if (realpath(javac_dir.c_str(), resolved_path) == nullptr) {
return "";
}
javac_dir = resolved_path;
Expand Down
22 changes: 10 additions & 12 deletions src/main/cpp/blaze_util_posix.cc
Expand Up @@ -190,7 +190,7 @@ void SignalHandler::Install(const string& product_name,
// Unblock all signals.
sigset_t sigset;
sigemptyset(&sigset);
sigprocmask(SIG_SETMASK, &sigset, NULL);
sigprocmask(SIG_SETMASK, &sigset, nullptr);

signal(SIGINT, handler);
signal(SIGTERM, handler);
Expand Down Expand Up @@ -255,7 +255,7 @@ class CharPP {
for (; i < args.size(); i++) {
charpp_[i] = strdup(args[i].c_str());
}
charpp_[i] = NULL;
charpp_[i] = nullptr;
}

// Constructs a new CharPP from a list of environment variables.
Expand All @@ -279,12 +279,12 @@ class CharPP {
assert(false);
}
}
charpp_[i] = NULL;
charpp_[i] = nullptr;
}

// Deletes all memory held by the CharPP.
~CharPP() {
for (char** ptr = charpp_; *ptr != NULL; ptr++) {
for (char** ptr = charpp_; *ptr != nullptr; ptr++) {
free(*ptr);
}
free(charpp_);
Expand Down Expand Up @@ -532,14 +532,12 @@ void CreateSecureOutputRoot(const blaze_util::Path& path) {

string GetEnv(const string& name) {
char* result = getenv(name.c_str());
return result != NULL ? string(result) : "";
return result != nullptr ? string(result) : "";
}

string GetPathEnv(const string& name) { return GetEnv(name); }

bool ExistsEnv(const string& name) {
return getenv(name.c_str()) != NULL;
}
bool ExistsEnv(const string& name) { return getenv(name.c_str()) != nullptr; }

void SetEnv(const string& name, const string& value) {
setenv(name.c_str(), value.c_str(), 1);
Expand All @@ -558,8 +556,8 @@ void SetupStdStreams() {
// output (when for example a query returns results as proto), in which case
// we must not perform line buffering on the client side. So turn off
// buffering here completely.
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
setvbuf(stdout, nullptr, _IONBF, 0);
setvbuf(stderr, nullptr, _IONBF, 0);

// Ensure we have three open fds. Otherwise we can end up with
// bizarre things like stdout going to the lock file, etc.
Expand Down Expand Up @@ -736,7 +734,7 @@ void TrySleep(unsigned int milliseconds) {
time_t seconds_part = (time_t)(milliseconds / 1000);
long nanoseconds_part = ((long)(milliseconds % 1000)) * 1000 * 1000;
struct timespec sleeptime = {seconds_part, nanoseconds_part};
nanosleep(&sleeptime, NULL);
nanosleep(&sleeptime, nullptr);
}

string GetUserName() {
Expand All @@ -746,7 +744,7 @@ string GetUserName() {
}
errno = 0;
passwd *pwent = getpwuid(getuid()); // NOLINT (single-threaded)
if (pwent == NULL || pwent->pw_name == NULL) {
if (pwent == nullptr || pwent->pw_name == nullptr) {
BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR)
<< "$USER is not set, and unable to look up name of current user: "
<< GetLastErrorString();
Expand Down

0 comments on commit deb1006

Please sign in to comment.