Imported Upstream version 1.7.1
[platform/upstream/ninja.git] / src / clparser.cc
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 #ifdef _WIN32
22 #include "includes_normalize.h"
23 #else
24 #include "util.h"
25 #endif
26
27 namespace {
28
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);
33 }
34
35 }  // anonymous namespace
36
37 // static
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) {
46     in += prefix.size();
47     while (*in == ' ')
48       ++in;
49     return line.substr(in - line.c_str());
50   }
51   return "";
52 }
53
54 // static
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);
60 }
61
62 // static
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");
70 }
71
72 // static
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);
77   size_t start = 0;
78   while (start < output.size()) {
79     size_t end = output.find_first_of("\r\n", start);
80     if (end == string::npos)
81       end = output.size();
82     string line = output.substr(start, end - start);
83
84     string include = FilterShowIncludes(line, deps_prefix);
85     if (!include.empty()) {
86       string normalized;
87 #ifdef _WIN32
88       if (!IncludesNormalize::Normalize(include, NULL, &normalized, err))
89         return false;
90 #else
91       // TODO: should this make the path relative to cwd?
92       normalized = include;
93       unsigned int slash_bits;
94       if (!CanonicalizePath(&normalized, &slash_bits, err))
95         return false;
96 #endif
97       if (!IsSystemInclude(normalized))
98         includes_.insert(normalized);
99     } else if (FilterInputFilename(line)) {
100       // Drop it.
101       // TODO: if we support compiling multiple output files in a single
102       // cl.exe invocation, we should stash the filename.
103     } else {
104       filtered_output->append(line);
105       filtered_output->append("\n");
106     }
107
108     if (end < output.size() && output[end] == '\r')
109       ++end;
110     if (end < output.size() && output[end] == '\n')
111       ++end;
112     start = end;
113   }
114
115   return true;
116 }