util: Remove unnecessary CanonicalizePath error handling

Since commit 86f606fe (Remove path component limit from input of
CanonicalizePath in windows, 2017-08-30, v1.8.0^2~2^2), the only failure
case in the `CanonicalizePath` implementation is the "empty path" error.
All call sites have been updated to ensure `CanonicalizePath` is never
called with an empty path.  Remove error handling from the signature to
simplify call sites.
This commit is contained in:
Brad King 2021-06-03 17:19:05 -04:00
parent f2f62e295a
commit 91706b323f
13 changed files with 103 additions and 151 deletions

View File

@ -878,9 +878,7 @@ bool Builder::ExtractDeps(CommandRunner::Result* result,
for (vector<StringPiece>::iterator i = deps.ins_.begin();
i != deps.ins_.end(); ++i) {
uint64_t slash_bits;
if (!CanonicalizePath(const_cast<char*>(i->str_), &i->len_, &slash_bits,
err))
return false;
CanonicalizePath(const_cast<char*>(i->str_), &i->len_, &slash_bits);
deps_nodes->push_back(state_->GetNode(*i, slash_bits));
}

View File

@ -26,7 +26,6 @@ const char kPath[] =
int main() {
vector<int> times;
string err;
char buf[200];
size_t len = strlen(kPath);
@ -37,7 +36,7 @@ int main() {
int64_t start = GetTimeMillis();
uint64_t slash_bits;
for (int i = 0; i < kNumRepetitions; ++i) {
CanonicalizePath(buf, &len, &slash_bits, &err);
CanonicalizePath(buf, &len, &slash_bits);
}
int delta = (int)(GetTimeMillis() - start);
times.push_back(delta);

View File

@ -195,20 +195,15 @@ int Cleaner::CleanTargets(int target_count, char* targets[]) {
continue;
}
uint64_t slash_bits;
string err;
if (!CanonicalizePath(&target_name, &slash_bits, &err)) {
Error("failed to canonicalize '%s': %s", target_name.c_str(), err.c_str());
status_ = 1;
CanonicalizePath(&target_name, &slash_bits);
Node* target = state_->LookupNode(target_name);
if (target) {
if (IsVerbose())
printf("Target %s\n", target_name.c_str());
DoCleanTarget(target);
} else {
Node* target = state_->LookupNode(target_name);
if (target) {
if (IsVerbose())
printf("Target %s\n", target_name.c_str());
DoCleanTarget(target);
} else {
Error("unknown target '%s'", target_name.c_str());
status_ = 1;
}
Error("unknown target '%s'", target_name.c_str());
status_ = 1;
}
}
PrintFooter();

View File

@ -103,8 +103,7 @@ bool CLParser::Parse(const string& output, const string& deps_prefix,
// TODO: should this make the path relative to cwd?
normalized = include;
uint64_t slash_bits;
if (!CanonicalizePath(&normalized, &slash_bits, err))
return false;
CanonicalizePath(&normalized, &slash_bits);
#endif
if (!IsSystemInclude(normalized))
includes_.insert(normalized);

View File

@ -117,10 +117,8 @@ bool DyndepParser::ParseEdge(string* err) {
string path = out0.Evaluate(&env_);
if (path.empty())
return lexer_.Error("empty path", err);
string path_err;
uint64_t slash_bits;
if (!CanonicalizePath(&path, &slash_bits, &path_err))
return lexer_.Error(path_err, err);
CanonicalizePath(&path, &slash_bits);
Node* node = state_->LookupNode(path);
if (!node || !node->in_edge())
return lexer_.Error("no build statement exists for '" + path + "'", err);
@ -206,10 +204,8 @@ bool DyndepParser::ParseEdge(string* err) {
string path = i->Evaluate(&env_);
if (path.empty())
return lexer_.Error("empty path", err);
string path_err;
uint64_t slash_bits;
if (!CanonicalizePath(&path, &slash_bits, &path_err))
return lexer_.Error(path_err, err);
CanonicalizePath(&path, &slash_bits);
Node* n = state_->GetNode(path, slash_bits);
dyndeps->implicit_inputs_.push_back(n);
}
@ -221,8 +217,7 @@ bool DyndepParser::ParseEdge(string* err) {
return lexer_.Error("empty path", err);
string path_err;
uint64_t slash_bits;
if (!CanonicalizePath(&path, &slash_bits, &path_err))
return lexer_.Error(path_err, err);
CanonicalizePath(&path, &slash_bits);
Node* n = state_->GetNode(path, slash_bits);
dyndeps->implicit_outputs_.push_back(n);
}

View File

@ -562,11 +562,8 @@ bool ImplicitDepLoader::LoadDepFile(Edge* edge, const string& path,
uint64_t unused;
std::vector<StringPiece>::iterator primary_out = depfile.outs_.begin();
if (!CanonicalizePath(const_cast<char*>(primary_out->str_),
&primary_out->len_, &unused, err)) {
*err = path + ": " + *err;
return false;
}
CanonicalizePath(const_cast<char*>(primary_out->str_), &primary_out->len_,
&unused);
// Check that this depfile matches the edge's output, if not return false to
// mark the edge as dirty.
@ -601,10 +598,7 @@ bool ImplicitDepLoader::ProcessDepfileDeps(
for (std::vector<StringPiece>::iterator i = depfile_ins->begin();
i != depfile_ins->end(); ++i, ++implicit_dep) {
uint64_t slash_bits;
if (!CanonicalizePath(const_cast<char*>(i->str_), &i->len_, &slash_bits,
err))
return false;
CanonicalizePath(const_cast<char*>(i->str_), &i->len_, &slash_bits);
Node* node = state_->GetNode(*i, slash_bits);
*implicit_dep = node;
node->AddOutEdge(edge);

View File

@ -191,8 +191,7 @@ bool IncludesNormalize::Normalize(const string& input,
}
strncpy(copy, input.c_str(), input.size() + 1);
uint64_t slash_bits;
if (!CanonicalizePath(copy, &len, &slash_bits, err))
return false;
CanonicalizePath(copy, &len, &slash_bits);
StringPiece partially_fixed(copy, len);
string abs_input = AbsPath(partially_fixed, err);
if (!err->empty())

View File

@ -192,12 +192,11 @@ bool ManifestParser::ParseDefault(string* err) {
string path = eval.Evaluate(env_);
if (path.empty())
return lexer_.Error("empty path", err);
string path_err;
uint64_t slash_bits; // Unused because this only does lookup.
if (!CanonicalizePath(&path, &slash_bits, &path_err))
return lexer_.Error(path_err, err);
if (!state_->AddDefault(path, &path_err))
return lexer_.Error(path_err, err);
CanonicalizePath(&path, &slash_bits);
std::string default_err;
if (!state_->AddDefault(path, &default_err))
return lexer_.Error(default_err, err);
eval.Clear();
if (!lexer_.ReadPath(&eval, err))
@ -321,10 +320,8 @@ bool ManifestParser::ParseEdge(string* err) {
string path = outs[i].Evaluate(env);
if (path.empty())
return lexer_.Error("empty path", err);
string path_err;
uint64_t slash_bits;
if (!CanonicalizePath(&path, &slash_bits, &path_err))
return lexer_.Error(path_err, err);
CanonicalizePath(&path, &slash_bits);
if (!state_->AddOut(edge, path, slash_bits)) {
if (options_.dupe_edge_action_ == kDupeEdgeActionError) {
lexer_.Error("multiple rules generate " + path, err);
@ -355,10 +352,8 @@ bool ManifestParser::ParseEdge(string* err) {
string path = i->Evaluate(env);
if (path.empty())
return lexer_.Error("empty path", err);
string path_err;
uint64_t slash_bits;
if (!CanonicalizePath(&path, &slash_bits, &path_err))
return lexer_.Error(path_err, err);
CanonicalizePath(&path, &slash_bits);
state_->AddIn(edge, path, slash_bits);
}
edge->implicit_deps_ = implicit;
@ -389,8 +384,7 @@ bool ManifestParser::ParseEdge(string* err) {
string dyndep = edge->GetUnescapedDyndep();
if (!dyndep.empty()) {
uint64_t slash_bits;
if (!CanonicalizePath(&dyndep, &slash_bits, err))
return false;
CanonicalizePath(&dyndep, &slash_bits);
edge->dyndep_ = state_->GetNode(dyndep, slash_bits);
edge->dyndep_->set_dyndep_pending(true);
vector<Node*>::iterator dgi =

View File

@ -52,9 +52,7 @@ bool NodeStoringImplicitDepLoader::ProcessDepfileDeps(
for (std::vector<StringPiece>::iterator i = depfile_ins->begin();
i != depfile_ins->end(); ++i) {
uint64_t slash_bits;
if (!CanonicalizePath(const_cast<char*>(i->str_), &i->len_, &slash_bits,
err))
return false;
CanonicalizePath(const_cast<char*>(i->str_), &i->len_, &slash_bits);
Node* node = state_->GetNode(*i, slash_bits);
dep_nodes_output_->push_back(node);
}

View File

@ -257,8 +257,7 @@ bool NinjaMain::RebuildManifest(const char* input_file, string* err,
return false;
}
uint64_t slash_bits; // Unused because this path is only used for lookup.
if (!CanonicalizePath(&path, &slash_bits, err))
return false;
CanonicalizePath(&path, &slash_bits);
Node* node = state_.LookupNode(path);
if (!node)
return false;
@ -293,8 +292,7 @@ Node* NinjaMain::CollectTarget(const char* cpath, string* err) {
return NULL;
}
uint64_t slash_bits;
if (!CanonicalizePath(&path, &slash_bits, err))
return NULL;
CanonicalizePath(&path, &slash_bits);
// Special syntax: "foo.cc^" means "the first output of foo.cc".
bool first_dependent = false;

View File

@ -117,16 +117,14 @@ void Info(const char* msg, ...) {
va_end(ap);
}
bool CanonicalizePath(string* path, uint64_t* slash_bits, string* err) {
void CanonicalizePath(string* path, uint64_t* slash_bits) {
METRIC_RECORD("canonicalize str");
size_t len = path->size();
char* str = 0;
if (len > 0)
str = &(*path)[0];
if (!CanonicalizePath(str, &len, slash_bits, err))
return false;
CanonicalizePath(str, &len, slash_bits);
path->resize(len);
return true;
}
static bool IsPathSeparator(char c) {
@ -137,14 +135,12 @@ static bool IsPathSeparator(char c) {
#endif
}
bool CanonicalizePath(char* path, size_t* len, uint64_t* slash_bits,
string* err) {
void CanonicalizePath(char* path, size_t* len, uint64_t* slash_bits) {
// WARNING: this function is performance-critical; please benchmark
// any changes you make to it.
METRIC_RECORD("canonicalize path");
if (*len == 0) {
*err = "empty path";
return false;
return;
}
const int kMaxPathComponents = 60;
@ -234,7 +230,6 @@ bool CanonicalizePath(char* path, size_t* len, uint64_t* slash_bits,
#else
*slash_bits = 0;
#endif
return true;
}
static inline bool IsKnownShellSafeCharacter(char ch) {

View File

@ -64,10 +64,8 @@ void Info(const char* msg, va_list ap);
/// Canonicalize a path like "foo/../bar.h" into just "bar.h".
/// |slash_bits| has bits set starting from lowest for a backslash that was
/// normalized to a forward slash. (only used on Windows)
bool CanonicalizePath(std::string* path, uint64_t* slash_bits,
std::string* err);
bool CanonicalizePath(char* path, size_t* len, uint64_t* slash_bits,
std::string* err);
void CanonicalizePath(std::string* path, uint64_t* slash_bits);
void CanonicalizePath(char* path, size_t* len, uint64_t* slash_bits);
/// Appends |input| to |*result|, escaping according to the whims of either
/// Bash, or Win32's CommandLineToArgvW().

View File

@ -20,70 +20,69 @@ using namespace std;
namespace {
bool CanonicalizePath(string* path, string* err) {
void CanonicalizePath(string* path) {
uint64_t unused;
return ::CanonicalizePath(path, &unused, err);
::CanonicalizePath(path, &unused);
}
} // namespace
TEST(CanonicalizePath, PathSamples) {
string path;
string err;
EXPECT_FALSE(CanonicalizePath(&path, &err));
EXPECT_EQ("empty path", err);
CanonicalizePath(&path);
EXPECT_EQ("", path);
path = "foo.h"; err = "";
EXPECT_TRUE(CanonicalizePath(&path, &err));
path = "foo.h";
CanonicalizePath(&path);
EXPECT_EQ("foo.h", path);
path = "./foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo.h", path);
path = "./foo/./bar.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo/bar.h", path);
path = "./x/foo/../bar.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("x/bar.h", path);
path = "./x/foo/../../bar.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("bar.h", path);
path = "foo//bar";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo/bar", path);
path = "foo//.//..///bar";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("bar", path);
path = "./x/../foo/../../bar.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("../bar.h", path);
path = "foo/./.";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo", path);
path = "foo/bar/..";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo", path);
path = "foo/.hidden_bar";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo/.hidden_bar", path);
path = "/foo";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("/foo", path);
path = "//foo";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
#ifdef _WIN32
EXPECT_EQ("//foo", path);
#else
@ -91,173 +90,171 @@ TEST(CanonicalizePath, PathSamples) {
#endif
path = "/";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("", path);
path = "/foo/..";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("", path);
path = ".";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ(".", path);
path = "./.";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ(".", path);
path = "foo/..";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ(".", path);
}
#ifdef _WIN32
TEST(CanonicalizePath, PathSamplesWindows) {
string path;
string err;
EXPECT_FALSE(CanonicalizePath(&path, &err));
EXPECT_EQ("empty path", err);
CanonicalizePath(&path);
EXPECT_EQ("", path);
path = "foo.h"; err = "";
EXPECT_TRUE(CanonicalizePath(&path, &err));
path = "foo.h";
CanonicalizePath(&path);
EXPECT_EQ("foo.h", path);
path = ".\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo.h", path);
path = ".\\foo\\.\\bar.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo/bar.h", path);
path = ".\\x\\foo\\..\\bar.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("x/bar.h", path);
path = ".\\x\\foo\\..\\..\\bar.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("bar.h", path);
path = "foo\\\\bar";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo/bar", path);
path = "foo\\\\.\\\\..\\\\\\bar";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("bar", path);
path = ".\\x\\..\\foo\\..\\..\\bar.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("../bar.h", path);
path = "foo\\.\\.";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo", path);
path = "foo\\bar\\..";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo", path);
path = "foo\\.hidden_bar";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("foo/.hidden_bar", path);
path = "\\foo";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("/foo", path);
path = "\\\\foo";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("//foo", path);
path = "\\";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("", path);
}
TEST(CanonicalizePath, SlashTracking) {
string path;
string err;
uint64_t slash_bits;
path = "foo.h"; err = "";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
path = "foo.h";
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("foo.h", path);
EXPECT_EQ(0, slash_bits);
path = "a\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/foo.h", path);
EXPECT_EQ(1, slash_bits);
path = "a/bcd/efh\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/bcd/efh/foo.h", path);
EXPECT_EQ(4, slash_bits);
path = "a\\bcd/efh\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/bcd/efh/foo.h", path);
EXPECT_EQ(5, slash_bits);
path = "a\\bcd\\efh\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/bcd/efh/foo.h", path);
EXPECT_EQ(7, slash_bits);
path = "a/bcd/efh/foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/bcd/efh/foo.h", path);
EXPECT_EQ(0, slash_bits);
path = "a\\./efh\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/efh/foo.h", path);
EXPECT_EQ(3, slash_bits);
path = "a\\../efh\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("efh/foo.h", path);
EXPECT_EQ(1, slash_bits);
path = "a\\b\\c\\d\\e\\f\\g\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/b/c/d/e/f/g/foo.h", path);
EXPECT_EQ(127, slash_bits);
path = "a\\b\\c\\..\\..\\..\\g\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("g/foo.h", path);
EXPECT_EQ(1, slash_bits);
path = "a\\b/c\\../../..\\g\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("g/foo.h", path);
EXPECT_EQ(1, slash_bits);
path = "a\\b/c\\./../..\\g\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/g/foo.h", path);
EXPECT_EQ(3, slash_bits);
path = "a\\b/c\\./../..\\g/foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/g/foo.h", path);
EXPECT_EQ(1, slash_bits);
path = "a\\\\\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/foo.h", path);
EXPECT_EQ(1, slash_bits);
path = "a/\\\\foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/foo.h", path);
EXPECT_EQ(0, slash_bits);
path = "a\\//foo.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ("a/foo.h", path);
EXPECT_EQ(1, slash_bits);
}
@ -266,22 +263,20 @@ TEST(CanonicalizePath, CanonicalizeNotExceedingLen) {
// Make sure searching \/ doesn't go past supplied len.
char buf[] = "foo/bar\\baz.h\\"; // Last \ past end.
uint64_t slash_bits;
string err;
size_t size = 13;
EXPECT_TRUE(::CanonicalizePath(buf, &size, &slash_bits, &err));
::CanonicalizePath(buf, &size, &slash_bits);
EXPECT_EQ(0, strncmp("foo/bar/baz.h", buf, size));
EXPECT_EQ(2, slash_bits); // Not including the trailing one.
}
TEST(CanonicalizePath, TooManyComponents) {
string path;
string err;
uint64_t slash_bits;
// 64 is OK.
path = "a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./"
"a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./x.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ(slash_bits, 0x0);
// Backslashes version.
@ -291,44 +286,40 @@ TEST(CanonicalizePath, TooManyComponents) {
"a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\"
"a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\x.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ(slash_bits, 0xffffffff);
// 65 is OK if #component is less than 60 after path canonicalization.
err = "";
path = "a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./"
"a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./a/./x/y.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ(slash_bits, 0x0);
// Backslashes version.
err = "";
path =
"a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\"
"a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\"
"a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\"
"a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\a\\.\\x\\y.h";
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ(slash_bits, 0x1ffffffff);
// 59 after canonicalization is OK.
err = "";
path = "a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/"
"a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/x/y.h";
EXPECT_EQ(58, std::count(path.begin(), path.end(), '/'));
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ(slash_bits, 0x0);
// Backslashes version.
err = "";
path =
"a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\"
"a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\"
"a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\"
"a\\a\\a\\a\\a\\a\\a\\a\\a\\x\\y.h";
EXPECT_EQ(58, std::count(path.begin(), path.end(), '\\'));
EXPECT_TRUE(CanonicalizePath(&path, &slash_bits, &err));
CanonicalizePath(&path, &slash_bits);
EXPECT_EQ(slash_bits, 0x3ffffffffffffff);
}
#endif
@ -336,36 +327,35 @@ TEST(CanonicalizePath, TooManyComponents) {
TEST(CanonicalizePath, UpDir) {
string path, err;
path = "../../foo/bar.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("../../foo/bar.h", path);
path = "test/../../foo/bar.h";
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("../foo/bar.h", path);
}
TEST(CanonicalizePath, AbsolutePath) {
string path = "/usr/include/stdio.h";
string err;
EXPECT_TRUE(CanonicalizePath(&path, &err));
CanonicalizePath(&path);
EXPECT_EQ("/usr/include/stdio.h", path);
}
TEST(CanonicalizePath, NotNullTerminated) {
string path;
string err;
size_t len;
uint64_t unused;
path = "foo/. bar/.";
len = strlen("foo/."); // Canonicalize only the part before the space.
EXPECT_TRUE(CanonicalizePath(&path[0], &len, &unused, &err));
CanonicalizePath(&path[0], &len, &unused);
EXPECT_EQ(strlen("foo"), len);
EXPECT_EQ("foo/. bar/.", string(path));
path = "foo/../file bar/.";
len = strlen("foo/../file");
EXPECT_TRUE(CanonicalizePath(&path[0], &len, &unused, &err));
CanonicalizePath(&path[0], &len, &unused);
EXPECT_EQ(strlen("file"), len);
EXPECT_EQ("file ./file bar/.", string(path));
}