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. 4 5 #include "gn/source_file.h" 6 7 #include "base/logging.h" 8 #include "gn/filesystem_utils.h" 9 #include "gn/source_dir.h" 10 #include "util/build_config.h" 11 12 namespace { 13 AssertValueSourceFileString(const std::string & s)14void 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; 22 } 23 GetSourceFileType(const std::string & file)24SourceFile::Type GetSourceFileType(const std::string& file) { 25 std::string_view extension = FindExtension(&file); 26 if (extension == "cc" || extension == "cpp" || extension == "cxx") 27 return SourceFile::SOURCE_CPP; 28 if (extension == "h" || extension == "hpp" || extension == "hxx" || 29 extension == "hh" || extension == "inc" || extension == "ipp" || 30 extension == "inl") 31 return SourceFile::SOURCE_H; 32 if (extension == "c") 33 return SourceFile::SOURCE_C; 34 if (extension == "m") 35 return SourceFile::SOURCE_M; 36 if (extension == "mm") 37 return SourceFile::SOURCE_MM; 38 if (extension == "rc") 39 return SourceFile::SOURCE_RC; 40 if (extension == "S" || extension == "s" || extension == "asm") 41 return SourceFile::SOURCE_S; 42 if (extension == "o" || extension == "obj") 43 return SourceFile::SOURCE_O; 44 if (extension == "def") 45 return SourceFile::SOURCE_DEF; 46 if (extension == "rs") 47 return SourceFile::SOURCE_RS; 48 if (extension == "go") 49 return SourceFile::SOURCE_GO; 50 51 return SourceFile::SOURCE_UNKNOWN; 52 } 53 Normalized(std::string value)54std::string Normalized(std::string value) { 55 DCHECK(!value.empty()); 56 AssertValueSourceFileString(value); 57 NormalizePath(&value); 58 return value; 59 } 60 61 } // namespace 62 SourceFile(const std::string & value)63SourceFile::SourceFile(const std::string& value) 64 : SourceFile(StringAtom(Normalized(value))) {} 65 SourceFile(std::string && value)66SourceFile::SourceFile(std::string&& value) 67 : SourceFile(StringAtom(Normalized(std::move(value)))) {} 68 SourceFile(StringAtom value)69SourceFile::SourceFile(StringAtom value) : value_(value) { 70 type_ = GetSourceFileType(value_.str()); 71 } 72 GetName() const73std::string SourceFile::GetName() const { 74 if (is_null()) 75 return std::string(); 76 77 const std::string& value = value_.str(); 78 DCHECK(value.find('/') != std::string::npos); 79 size_t last_slash = value.rfind('/'); 80 return std::string(&value[last_slash + 1], value.size() - last_slash - 1); 81 } 82 GetDir() const83SourceDir SourceFile::GetDir() const { 84 if (is_null()) 85 return SourceDir(); 86 87 const std::string& value = value_.str(); 88 DCHECK(value.find('/') != std::string::npos); 89 size_t last_slash = value.rfind('/'); 90 return SourceDir(value.substr(0, last_slash + 1)); 91 } 92 Resolve(const base::FilePath & source_root) const93base::FilePath SourceFile::Resolve(const base::FilePath& source_root) const { 94 return ResolvePath(value_.str(), true, source_root); 95 } 96 SetValue(const std::string & value)97void SourceFile::SetValue(const std::string& value) { 98 value_ = StringAtom(value); 99 type_ = GetSourceFileType(value); 100 } 101 SourceFileTypeSet()102SourceFileTypeSet::SourceFileTypeSet() : empty_(true) { 103 memset(flags_, 0, 104 sizeof(bool) * static_cast<int>(SourceFile::SOURCE_NUMTYPES)); 105 } 106 CSourceUsed() const107bool SourceFileTypeSet::CSourceUsed() const { 108 return empty_ || Get(SourceFile::SOURCE_CPP) || Get(SourceFile::SOURCE_H) || 109 Get(SourceFile::SOURCE_C) || Get(SourceFile::SOURCE_M) || 110 Get(SourceFile::SOURCE_MM) || Get(SourceFile::SOURCE_RC) || 111 Get(SourceFile::SOURCE_S) || Get(SourceFile::SOURCE_O) || 112 Get(SourceFile::SOURCE_DEF); 113 } 114 RustSourceUsed() const115bool SourceFileTypeSet::RustSourceUsed() const { 116 return Get(SourceFile::SOURCE_RS); 117 } 118 GoSourceUsed() const119bool SourceFileTypeSet::GoSourceUsed() const { 120 return Get(SourceFile::SOURCE_GO); 121 } 122 MixedSourceUsed() const123bool SourceFileTypeSet::MixedSourceUsed() const { 124 return (1 << static_cast<int>(CSourceUsed()) 125 << static_cast<int>(RustSourceUsed()) 126 << static_cast<int>(GoSourceUsed())) > 2; 127 } 128