1 // Copyright 2015 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 
15 #include "clparser.h"
16 
17 #include <algorithm>
18 #include <assert.h>
19 #include <string.h>
20 
21 #include "metrics.h"
22 #include "string_piece_util.h"
23 
24 #ifdef _WIN32
25 #include "includes_normalize.h"
26 #include "string_piece.h"
27 #else
28 #include "util.h"
29 #endif
30 
31 namespace {
32 
33 /// Return true if \a input ends with \a needle.
EndsWith(const string & input,const string & needle)34 bool EndsWith(const string& input, const string& needle) {
35   return (input.size() >= needle.size() &&
36           input.substr(input.size() - needle.size()) == needle);
37 }
38 
39 }  // anonymous namespace
40 
41 // static
FilterShowIncludes(const string & line,const string & deps_prefix)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) {
50     in += prefix.size();
51     while (*in == ' ')
52       ++in;
53     return line.substr(in - line.c_str());
54   }
55   return "";
56 }
57 
58 // static
IsSystemInclude(string path)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);
64 }
65 
66 // static
FilterInputFilename(string line)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");
74 }
75 
76 // static
Parse(const string & output,const string & deps_prefix,string * filtered_output,string * err)77 bool CLParser::Parse(const string& output, const string& deps_prefix,
78                      string* filtered_output, string* err) {
79   METRIC_RECORD("CLParser::Parse");
80 
81   // Loop over all lines in the output to process them.
82   assert(&output != filtered_output);
83   size_t start = 0;
84 #ifdef _WIN32
85   IncludesNormalize normalizer(".");
86 #endif
87 
88   while (start < output.size()) {
89     size_t end = output.find_first_of("\r\n", start);
90     if (end == string::npos)
91       end = output.size();
92     string line = output.substr(start, end - start);
93 
94     string include = FilterShowIncludes(line, deps_prefix);
95     if (!include.empty()) {
96       string normalized;
97 #ifdef _WIN32
98       if (!normalizer.Normalize(include, &normalized, err))
99         return false;
100 #else
101       // TODO: should this make the path relative to cwd?
102       normalized = include;
103       uint64_t slash_bits;
104       if (!CanonicalizePath(&normalized, &slash_bits, err))
105         return false;
106 #endif
107       if (!IsSystemInclude(normalized))
108         includes_.insert(normalized);
109     } else if (FilterInputFilename(line)) {
110       // Drop it.
111       // TODO: if we support compiling multiple output files in a single
112       // cl.exe invocation, we should stash the filename.
113     } else {
114       filtered_output->append(line);
115       filtered_output->append("\n");
116     }
117 
118     if (end < output.size() && output[end] == '\r')
119       ++end;
120     if (end < output.size() && output[end] == '\n')
121       ++end;
122     start = end;
123   }
124 
125   return true;
126 }
127