mirror of
https://github.com/reactos/ninja.git
synced 2024-11-23 03:39:48 +00:00
Merge pull request #1979 from bradking/simplify-CanonicalizePath
util: Remove unnecessary CanonicalizePath error handling
This commit is contained in:
commit
d68f107f7a
@ -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));
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
26
src/clean.cc
26
src/clean.cc
@ -189,21 +189,21 @@ int Cleaner::CleanTargets(int target_count, char* targets[]) {
|
||||
LoadDyndeps();
|
||||
for (int i = 0; i < target_count; ++i) {
|
||||
string target_name = targets[i];
|
||||
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());
|
||||
if (target_name.empty()) {
|
||||
Error("failed to canonicalize '': empty path");
|
||||
status_ = 1;
|
||||
continue;
|
||||
}
|
||||
uint64_t slash_bits;
|
||||
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();
|
||||
|
@ -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);
|
||||
|
@ -115,10 +115,10 @@ bool DyndepParser::ParseEdge(string* err) {
|
||||
return lexer_.Error("expected path", err);
|
||||
|
||||
string path = out0.Evaluate(&env_);
|
||||
string path_err;
|
||||
if (path.empty())
|
||||
return lexer_.Error("empty 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);
|
||||
@ -202,10 +202,10 @@ bool DyndepParser::ParseEdge(string* err) {
|
||||
dyndeps->implicit_inputs_.reserve(ins.size());
|
||||
for (vector<EvalString>::iterator i = ins.begin(); i != ins.end(); ++i) {
|
||||
string path = i->Evaluate(&env_);
|
||||
string path_err;
|
||||
if (path.empty())
|
||||
return lexer_.Error("empty 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);
|
||||
}
|
||||
@ -213,10 +213,11 @@ bool DyndepParser::ParseEdge(string* err) {
|
||||
dyndeps->implicit_outputs_.reserve(outs.size());
|
||||
for (vector<EvalString>::iterator i = outs.begin(); i != outs.end(); ++i) {
|
||||
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_outputs_.push_back(n);
|
||||
}
|
||||
|
12
src/graph.cc
12
src/graph.cc
@ -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);
|
||||
|
@ -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())
|
||||
|
@ -190,12 +190,13 @@ bool ManifestParser::ParseDefault(string* err) {
|
||||
|
||||
do {
|
||||
string path = eval.Evaluate(env_);
|
||||
string path_err;
|
||||
if (path.empty())
|
||||
return lexer_.Error("empty 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))
|
||||
@ -317,10 +318,10 @@ bool ManifestParser::ParseEdge(string* err) {
|
||||
edge->outputs_.reserve(outs.size());
|
||||
for (size_t i = 0, e = outs.size(); i != e; ++i) {
|
||||
string path = outs[i].Evaluate(env);
|
||||
string path_err;
|
||||
if (path.empty())
|
||||
return lexer_.Error("empty 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);
|
||||
@ -349,10 +350,10 @@ bool ManifestParser::ParseEdge(string* err) {
|
||||
edge->inputs_.reserve(ins.size());
|
||||
for (vector<EvalString>::iterator i = ins.begin(); i != ins.end(); ++i) {
|
||||
string path = i->Evaluate(env);
|
||||
string path_err;
|
||||
if (path.empty())
|
||||
return lexer_.Error("empty 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;
|
||||
@ -383,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 =
|
||||
|
@ -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);
|
||||
}
|
||||
|
14
src/ninja.cc
14
src/ninja.cc
@ -252,9 +252,12 @@ int GuessParallelism() {
|
||||
bool NinjaMain::RebuildManifest(const char* input_file, string* err,
|
||||
Status* status) {
|
||||
string path = input_file;
|
||||
uint64_t slash_bits; // Unused because this path is only used for lookup.
|
||||
if (!CanonicalizePath(&path, &slash_bits, err))
|
||||
if (path.empty()) {
|
||||
*err = "empty path";
|
||||
return false;
|
||||
}
|
||||
uint64_t slash_bits; // Unused because this path is only used for lookup.
|
||||
CanonicalizePath(&path, &slash_bits);
|
||||
Node* node = state_.LookupNode(path);
|
||||
if (!node)
|
||||
return false;
|
||||
@ -284,9 +287,12 @@ bool NinjaMain::RebuildManifest(const char* input_file, string* err,
|
||||
|
||||
Node* NinjaMain::CollectTarget(const char* cpath, string* err) {
|
||||
string path = cpath;
|
||||
uint64_t slash_bits;
|
||||
if (!CanonicalizePath(&path, &slash_bits, err))
|
||||
if (path.empty()) {
|
||||
*err = "empty path";
|
||||
return NULL;
|
||||
}
|
||||
uint64_t slash_bits;
|
||||
CanonicalizePath(&path, &slash_bits);
|
||||
|
||||
// Special syntax: "foo.cc^" means "the first output of foo.cc".
|
||||
bool first_dependent = false;
|
||||
|
13
src/util.cc
13
src/util.cc
@ -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) {
|
||||
|
@ -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().
|
||||
|
148
src/util_test.cc
148
src/util_test.cc
@ -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));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user