* Changing crash report limitation logic to have more control over extreme cases.
[chromium-blink-merge.git] / tools / gn / source_file.cc
blob41a602d2f01f0d8c293a5cadb05a329519b3bb33
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "tools/gn/source_file.h"
7 #include "base/logging.h"
8 #include "build/build_config.h"
9 #include "tools/gn/filesystem_utils.h"
10 #include "tools/gn/source_dir.h"
12 namespace {
14 void AssertValueSourceFileString(const std::string& s) {
15 #if defined(OS_WIN)
16 DCHECK(s[0] == '/' ||
17 (s.size() > 2 && s[0] != '/' && s[1] == ':' && IsSlash(s[2])));
18 #else
19 DCHECK(s[0] == '/');
20 #endif
21 DCHECK(!EndsWithSlash(s)) << s;
24 } // namespace
26 SourceFile::SourceFile() {
29 SourceFile::SourceFile(const base::StringPiece& p)
30 : value_(p.data(), p.size()) {
31 DCHECK(!value_.empty());
32 AssertValueSourceFileString(value_);
33 NormalizePath(&value_);
36 SourceFile::SourceFile(SwapIn, std::string* value) {
37 value_.swap(*value);
38 DCHECK(!value_.empty());
39 AssertValueSourceFileString(value_);
40 NormalizePath(&value_);
43 SourceFile::~SourceFile() {
46 std::string SourceFile::GetName() const {
47 if (is_null())
48 return std::string();
50 DCHECK(value_.find('/') != std::string::npos);
51 size_t last_slash = value_.rfind('/');
52 return std::string(&value_[last_slash + 1],
53 value_.size() - last_slash - 1);
56 SourceDir SourceFile::GetDir() const {
57 if (is_null())
58 return SourceDir();
60 DCHECK(value_.find('/') != std::string::npos);
61 size_t last_slash = value_.rfind('/');
62 return SourceDir(base::StringPiece(&value_[0], last_slash + 1));
65 base::FilePath SourceFile::Resolve(const base::FilePath& source_root) const {
66 if (is_null())
67 return base::FilePath();
69 std::string converted;
70 if (is_system_absolute()) {
71 if (value_.size() > 2 && value_[2] == ':') {
72 // Windows path, strip the leading slash.
73 converted.assign(&value_[1], value_.size() - 1);
74 } else {
75 converted.assign(value_);
77 return base::FilePath(UTF8ToFilePath(converted));
80 converted.assign(&value_[2], value_.size() - 2);
81 if (source_root.empty())
82 return UTF8ToFilePath(converted).NormalizePathSeparatorsTo('/');
83 return source_root.Append(UTF8ToFilePath(converted))
84 .NormalizePathSeparatorsTo('/');