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 "string_piece_util.h"
25 #include "includes_normalize.h"
26 #include "string_piece.h"
33 /// Return true if \a input ends with \a needle.
34 bool EndsWith(const string& input, const string& needle) {
35 return (input.size() >= needle.size() &&
36 input.substr(input.size() - needle.size()) == needle);
39 } // anonymous namespace
42 string CLParser::FilterShowIncludes(const string& line,
43 const string& deps_prefix) {
44 const string kDepsPrefixEnglish = "Note: including file: ";
45 const char* in = line.c_str();
46 const char* end = in + line.size();
47 const string& prefix = deps_prefix.empty() ? kDepsPrefixEnglish : deps_prefix;
48 if (end - in > (int)prefix.size() &&
49 memcmp(in, prefix.c_str(), (int)prefix.size()) == 0) {
53 return line.substr(in - line.c_str());
59 bool CLParser::IsSystemInclude(string path) {
60 transform(path.begin(), path.end(), path.begin(), ToLowerASCII);
61 // TODO: this is a heuristic, perhaps there's a better way?
62 return (path.find("program files") != string::npos ||
63 path.find("microsoft visual studio") != string::npos);
67 bool CLParser::FilterInputFilename(string line) {
68 transform(line.begin(), line.end(), line.begin(), ToLowerASCII);
69 // TODO: other extensions, like .asm?
70 return EndsWith(line, ".c") ||
71 EndsWith(line, ".cc") ||
72 EndsWith(line, ".cxx") ||
73 EndsWith(line, ".cpp");
77 bool CLParser::Parse(const string& output, const string& deps_prefix,
78 string* filtered_output, string* err) {
79 METRIC_RECORD("CLParser::Parse");
81 // Loop over all lines in the output to process them.
82 assert(&output != filtered_output);
85 IncludesNormalize normalizer(".");
88 while (start < output.size()) {
89 size_t end = output.find_first_of("\r\n", start);
90 if (end == string::npos)
92 string line = output.substr(start, end - start);
94 string include = FilterShowIncludes(line, deps_prefix);
95 if (!include.empty()) {
98 if (!normalizer.Normalize(include, &normalized, err))
101 // TODO: should this make the path relative to cwd?
102 normalized = include;
104 if (!CanonicalizePath(&normalized, &slash_bits, err))
107 if (!IsSystemInclude(normalized))
108 includes_.insert(normalized);
109 } else if (FilterInputFilename(line)) {
111 // TODO: if we support compiling multiple output files in a single
112 // cl.exe invocation, we should stash the filename.
114 filtered_output->append(line);
115 filtered_output->append("\n");
118 if (end < output.size() && output[end] == '\r')
120 if (end < output.size() && output[end] == '\n')