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 using namespace std;
32 
33 namespace {
34 
35 /// Return true if \a input ends with \a needle.
EndsWith(const string & input,const string & needle)36 bool EndsWith(const string& input, const string& needle) {
37   return (input.size() >= needle.size() &&
38           input.substr(input.size() - needle.size()) == needle);
39 }
40 
41 }  // anonymous namespace
42 
43 // static
FilterShowIncludes(const string & line,const string & deps_prefix)44 string CLParser::FilterShowIncludes(const string& line,
45                                     const string& deps_prefix) {
46   const string kDepsPrefixEnglish = "Note: including file: ";
47   const char* in = line.c_str();
48   const char* end = in + line.size();
49   const string& prefix = deps_prefix.empty() ? kDepsPrefixEnglish : deps_prefix;
50   if (end - in > (int)prefix.size() &&
51       memcmp(in, prefix.c_str(), (int)prefix.size()) == 0) {
52     in += prefix.size();
53     while (*in == ' ')
54       ++in;
55     return line.substr(in - line.c_str());
56   }
57   return "";
58 }
59 
60 // static
IsSystemInclude(string path)61 bool CLParser::IsSystemInclude(string path) {
62   transform(path.begin(), path.end(), path.begin(), ToLowerASCII);
63   // TODO: this is a heuristic, perhaps there's a better way?
64   return (path.find("program files") != string::npos ||
65           path.find("microsoft visual studio") != string::npos);
66 }
67 
68 // static
FilterInputFilename(string line)69 bool CLParser::FilterInputFilename(string line) {
70   transform(line.begin(), line.end(), line.begin(), ToLowerASCII);
71   // TODO: other extensions, like .asm?
72   return EndsWith(line, ".c") ||
73       EndsWith(line, ".cc") ||
74       EndsWith(line, ".cxx") ||
75       EndsWith(line, ".cpp");
76 }
77 
78 // static
Parse(const string & output,const string & deps_prefix,string * filtered_output,string * err)79 bool CLParser::Parse(const string& output, const string& deps_prefix,
80                      string* filtered_output, string* err) {
81   METRIC_RECORD("CLParser::Parse");
82 
83   // Loop over all lines in the output to process them.
84   assert(&output != filtered_output);
85   size_t start = 0;
86 #ifdef _WIN32
87   IncludesNormalize normalizer(".");
88 #endif
89 
90   while (start < output.size()) {
91     size_t end = output.find_first_of("\r\n", start);
92     if (end == string::npos)
93       end = output.size();
94     string line = output.substr(start, end - start);
95 
96     string include = FilterShowIncludes(line, deps_prefix);
97     if (!include.empty()) {
98       string normalized;
99 #ifdef _WIN32
100       if (!normalizer.Normalize(include, &normalized, err))
101         return false;
102 #else
103       // TODO: should this make the path relative to cwd?
104       normalized = include;
105       uint64_t slash_bits;
106       if (!CanonicalizePath(&normalized, &slash_bits, err))
107         return false;
108 #endif
109       if (!IsSystemInclude(normalized))
110         includes_.insert(normalized);
111     } else if (FilterInputFilename(line)) {
112       // Drop it.
113       // TODO: if we support compiling multiple output files in a single
114       // cl.exe invocation, we should stash the filename.
115     } else {
116       filtered_output->append(line);
117       filtered_output->append("\n");
118     }
119 
120     if (end < output.size() && output[end] == '\r')
121       ++end;
122     if (end < output.size() && output[end] == '\n')
123       ++end;
124     start = end;
125   }
126 
127   return true;
128 }
129