1 //===--- iwyu_path_util.h - file-path utilities for include-what-you-use --===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // File-path utilities for the IWYU checker. 11 12 #ifndef INCLUDE_WHAT_YOU_USE_IWYU_PATH_UTIL_H_ 13 #define INCLUDE_WHAT_YOU_USE_IWYU_PATH_UTIL_H_ 14 15 #include <string> // for string, allocator, etc 16 #include <vector> 17 18 #include "iwyu_string_util.h" 19 20 namespace include_what_you_use { 21 22 using std::string; 23 using std::vector; 24 25 26 // One entry in the search-path list of where to find #include files. 27 struct HeaderSearchPath { 28 enum Type { kUnusedPath = 0, kSystemPath, kUserPath }; HeaderSearchPathHeaderSearchPath29 HeaderSearchPath(const string& p, Type pt) : path(p), path_type(pt) { } 30 string path; // the path-entry as specified on the commandline (via -I) 31 Type path_type; 32 }; 33 34 // The directories to look for #includes in, including from -I, -isystem, etc. 35 void SetHeaderSearchPaths(const vector<HeaderSearchPath>& search_paths); 36 const vector<HeaderSearchPath>& HeaderSearchPaths(); 37 38 // Returns true if 'path' is a path of a (possibly enclosed in double 39 // quotes or <>) C++ header file. 40 bool IsHeaderFile(string path); 41 42 // If the path has a slash, return the part after the last slash, 43 // else return the input path. 44 string Basename(const string& path); 45 46 // Normalizes the file path, then strips uninteresting suffixes from 47 // the file name. Replaces "/internal/" with "/public/" and 48 // "/include/" with "/src". 49 string GetCanonicalName(string file_path); 50 51 // Replaces "\" by "/" (Microsoft platform paths) and collapses all dot 52 // components in path. 53 string NormalizeFilePath(const string& path); 54 55 // Normalizes like NormalizeFilePath and ensures trailing slash. 56 // Hence use only for directories! 57 string NormalizeDirPath(const string& path); 58 59 // Is path absolute? 60 bool IsAbsolutePath(const string& path); 61 62 // Get absolute version of path. 63 string MakeAbsolutePath(const string& path); 64 string MakeAbsolutePath(const string& base_path, const string& relative_path); 65 66 // Get the parent of path. 67 string GetParentPath(const string& path); 68 69 // Try to strip the prefix_path from the front of path. 70 // The path assumed to be normalized but either absolute or relative. 71 // Return true if path was stripped. 72 bool StripPathPrefix(string* path, const string& prefix_path); 73 74 // Below, we talk 'quoted' includes. A quoted include is something 75 // that would be written on an #include line, complete with the <> or 76 // "". In the line '#include <time.h>', "<time.h>" is the quoted 77 // include. 78 79 // Converts a file-path, such as /usr/include/stdio.h, to a 80 // quoted include, such as <stdio.h>. 81 string ConvertToQuotedInclude(const string& filepath, 82 const string& includer_path = ""); 83 84 // Returns true if the string is a quoted include. 85 bool IsQuotedInclude(const string& s); 86 87 // Returns whether this is a system (as opposed to user) include 88 // file, based on where it lives. 89 bool IsSystemIncludeFile(const string& filepath); 90 91 } // namespace include_what_you_use 92 93 #endif // INCLUDE_WHAT_YOU_USE_IWYU_PATH_UTIL_H_ 94