28 bool EndsWith(
const string& input,
const string& needle) {
29 return (input.size() >= needle.size() &&
30 input.substr(input.size() - needle.size()) == needle);
33 string Replace(
const string& input,
const string& find,
const string& replace) {
34 string result = input;
36 while ((start_pos = result.find(find, start_pos)) != string::npos) {
37 result.replace(start_pos, find.length(), replace);
38 start_pos += replace.length();
47 return Replace(path,
" ",
"\\ ");
52 static const char kMagicPrefix[] =
"Note: including file: ";
53 const char* in = line.c_str();
54 const char* end = in + line.size();
56 if (end - in > (
int)
sizeof(kMagicPrefix) - 1 &&
57 memcmp(in, kMagicPrefix,
sizeof(kMagicPrefix) - 1) == 0) {
58 in +=
sizeof(kMagicPrefix) - 1;
61 return line.substr(in - line.c_str());
68 transform(path.begin(), path.end(), path.begin(), ::tolower);
70 return (path.find(
"program files") != string::npos ||
71 path.find(
"microsoft visual studio") != string::npos);
76 transform(line.begin(), line.end(), line.begin(), ::tolower);
78 return EndsWith(line,
".c") ||
79 EndsWith(line,
".cc") ||
80 EndsWith(line,
".cxx") ||
81 EndsWith(line,
".cpp");
85 string filtered_output;
89 while (start < output.size()) {
90 size_t end = output.find_first_of(
"\r\n", start);
91 if (end == string::npos)
93 string line = output.substr(start, end - start);
96 if (!include.empty()) {
105 filtered_output.append(line);
106 filtered_output.append(
"\n");
109 if (end < output.size() && output[end] ==
'\r')
111 if (end < output.size() && output[end] ==
'\n')
116 return filtered_output;
120 SECURITY_ATTRIBUTES security_attributes = {};
121 security_attributes.nLength =
sizeof(SECURITY_ATTRIBUTES);
122 security_attributes.bInheritHandle = TRUE;
125 HANDLE nul = CreateFile(
"NUL", GENERIC_READ,
126 FILE_SHARE_READ | FILE_SHARE_WRITE |
128 &security_attributes, OPEN_EXISTING, 0, NULL);
129 if (nul == INVALID_HANDLE_VALUE)
130 Fatal(
"couldn't open nul");
132 HANDLE stdout_read, stdout_write;
133 if (!CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0))
134 Win32Fatal(
"CreatePipe");
136 if (!SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0))
137 Win32Fatal(
"SetHandleInformation");
139 PROCESS_INFORMATION process_info = {};
140 STARTUPINFO startup_info = {};
141 startup_info.cb =
sizeof(STARTUPINFO);
142 startup_info.hStdInput = nul;
143 startup_info.hStdError = stdout_write;
144 startup_info.hStdOutput = stdout_write;
145 startup_info.dwFlags |= STARTF_USESTDHANDLES;
147 if (!CreateProcessA(NULL, (
char*)command.c_str(), NULL, NULL,
150 &startup_info, &process_info)) {
151 Win32Fatal(
"CreateProcess");
154 if (!CloseHandle(nul) ||
155 !CloseHandle(stdout_write)) {
156 Win32Fatal(
"CloseHandle");
164 if (!::
ReadFile(stdout_read, buf,
sizeof(buf), &read_len, NULL) &&
165 GetLastError() != ERROR_BROKEN_PIPE) {
166 Win32Fatal(
"ReadFile");
168 output->append(buf, read_len);
172 if (WaitForSingleObject(process_info.hProcess, INFINITE) == WAIT_FAILED)
173 Win32Fatal(
"WaitForSingleObject");
175 if (!GetExitCodeProcess(process_info.hProcess, &exit_code))
176 Win32Fatal(
"GetExitCodeProcess");
178 if (!CloseHandle(stdout_read) ||
179 !CloseHandle(process_info.hProcess) ||
180 !CloseHandle(process_info.hThread)) {
181 Win32Fatal(
"CloseHandle");
static string FilterShowIncludes(const string &line)
Parse a line of cl.exe output and extract /showIncludes info.
static bool FilterInputFilename(string line)
Parse a line of cl.exe output and return true if it looks like it's printing an input filename...
static bool IsSystemInclude(string path)
Return true if a mentioned include file is a system path.
static string Normalize(const string &input, const char *relative_to)
Normalize by fixing slashes style, fixing redundant .
string Parse(const string &output)
Parse the full output of cl, returning the output (if any) that should printed.
int ReadFile(const string &path, string *contents, string *err)
Read a file to a string (in text mode: with CRLF conversion on Windows).
int Run(const string &command, string *output)
Start a process and gather its raw output.
void Fatal(const char *msg,...)
Log a fatal message and exit.
string EscapeForDepfile(const string &path)