1 // Copyright 2015 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
22 #include "includes_normalize.h"
29 /// Return true if \a input ends with \a needle.
30 bool EndsWith(const string& input, const string& needle) {
31 return (input.size() >= needle.size() &&
32 input.substr(input.size() - needle.size()) == needle);
35 } // anonymous namespace
38 string CLParser::FilterShowIncludes(const string& line,
39 const string& deps_prefix) {
40 const string kDepsPrefixEnglish = "Note: including file: ";
41 const char* in = line.c_str();
42 const char* end = in + line.size();
43 const string& prefix = deps_prefix.empty() ? kDepsPrefixEnglish : deps_prefix;
44 if (end - in > (int)prefix.size() &&
45 memcmp(in, prefix.c_str(), (int)prefix.size()) == 0) {
49 return line.substr(in - line.c_str());
55 bool CLParser::IsSystemInclude(string path) {
56 transform(path.begin(), path.end(), path.begin(), ::tolower);
57 // TODO: this is a heuristic, perhaps there's a better way?
58 return (path.find("program files") != string::npos ||
59 path.find("microsoft visual studio") != string::npos);
63 bool CLParser::FilterInputFilename(string line) {
64 transform(line.begin(), line.end(), line.begin(), ::tolower);
65 // TODO: other extensions, like .asm?
66 return EndsWith(line, ".c") ||
67 EndsWith(line, ".cc") ||
68 EndsWith(line, ".cxx") ||
69 EndsWith(line, ".cpp");
73 bool CLParser::Parse(const string& output, const string& deps_prefix,
74 string* filtered_output, string* err) {
75 // Loop over all lines in the output to process them.
76 assert(&output != filtered_output);
78 while (start < output.size()) {
79 size_t end = output.find_first_of("\r\n", start);
80 if (end == string::npos)
82 string line = output.substr(start, end - start);
84 string include = FilterShowIncludes(line, deps_prefix);
85 if (!include.empty()) {
88 if (!IncludesNormalize::Normalize(include, NULL, &normalized, err))
91 // TODO: should this make the path relative to cwd?
93 unsigned int slash_bits;
94 if (!CanonicalizePath(&normalized, &slash_bits, err))
97 if (!IsSystemInclude(normalized))
98 includes_.insert(normalized);
99 } else if (FilterInputFilename(line)) {
101 // TODO: if we support compiling multiple output files in a single
102 // cl.exe invocation, we should stash the filename.
104 filtered_output->append(line);
105 filtered_output->append("\n");
108 if (end < output.size() && output[end] == '\r')
110 if (end < output.size() && output[end] == '\n')