Cppcheck
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
preprocessor.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 #include "preprocessor.h"
21 #include "tokenize.h"
22 #include "token.h"
23 #include "path.h"
24 #include "errorlogger.h"
25 #include "settings.h"
26 
27 #include <algorithm>
28 #include <sstream>
29 #include <fstream>
30 #include <cstdlib>
31 #include <cctype>
32 #include <vector>
33 #include <set>
34 #include <stack>
35 
38 
39 char Preprocessor::macroChar = char(1);
40 
41 Preprocessor::Preprocessor(Settings *settings, ErrorLogger *errorLogger) : _settings(settings), _errorLogger(errorLogger), _foundUnhandledChars(false)
42 {
43 
44 }
45 
46 void Preprocessor::writeError(const std::string &fileName, const unsigned int linenr, ErrorLogger *errorLogger, const std::string &errorType, const std::string &errorText)
47 {
48  if (!errorLogger)
49  return;
50 
51  std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
53  loc.line = linenr;
54  loc.setfile(fileName);
55  locationList.push_back(loc);
56  errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList,
58  errorText,
59  errorType,
60  false));
61 }
62 
63 static unsigned char readChar(std::istream &istr, unsigned int bom)
64 {
65  unsigned char ch = (unsigned char)istr.get();
66 
67  // For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the
68  // character is non-ASCII character then replace it with 0xff
69  if (bom == 0xfeff || bom == 0xfffe) {
70  unsigned char ch2 = (unsigned char)istr.get();
71  int ch16 = (bom == 0xfeff) ? (ch<<8 | ch2) : (ch2<<8 | ch);
72  ch = (unsigned char)((ch16 >= 0x80) ? 0xff : ch16);
73  }
74 
75  // Handling of newlines..
76  if (ch == '\r') {
77  ch = '\n';
78  if (bom == 0 && (char)istr.peek() == '\n')
79  (void)istr.get();
80  else if (bom == 0xfeff || bom == 0xfffe) {
81  int c1 = istr.get();
82  int c2 = istr.get();
83  int ch16 = (bom == 0xfeff) ? (c1<<8 | c2) : (c2<<8 | c1);
84  if (ch16 != '\n') {
85  istr.unget();
86  istr.unget();
87  }
88  }
89  }
90 
91  return ch;
92 }
93 
94 // Concatenates a list of strings, inserting a separator between parts
95 static std::string join(const std::set<std::string>& list, char separator)
96 {
97  std::string s;
98  for (std::set<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
99  if (!s.empty())
100  s += separator;
101 
102  s += *it;
103  }
104  return s;
105 }
106 
107 // Removes duplicate string portions separated by the specified separator
108 static std::string unify(const std::string &s, char separator)
109 {
110  std::set<std::string> parts;
111 
112  std::string::size_type prevPos = 0;
113  for (std::string::size_type pos = 0; pos < s.length(); ++pos) {
114  if (s[pos] == separator) {
115  if (pos > prevPos)
116  parts.insert(s.substr(prevPos, pos - prevPos));
117  prevPos = pos + 1;
118  }
119  }
120  if (prevPos < s.length())
121  parts.insert(s.substr(prevPos));
122 
123  return join(parts, separator);
124 }
125 
126 
127 bool Preprocessor::cplusplus(const Settings *settings, const std::string &filename)
128 {
129  const bool undef = settings && settings->userUndefs.find("__cplusplus") != settings->userUndefs.end();
130  const bool cpplang = settings && settings->enforcedLang == Settings::CPP;
131  const bool cppfile = (!settings || settings->enforcedLang == Settings::None) && Path::isCPP(filename);
132  return (!undef && (cpplang || cppfile));
133 }
134 
135 /**
136  * Get cfgmap - a map of macro names and values
137  */
138 static std::map<std::string,std::string> getcfgmap(const std::string &cfg, const Settings *settings, const std::string &filename)
139 {
140  std::map<std::string, std::string> cfgmap;
141 
142  if (!cfg.empty()) {
143  std::string::size_type pos = 0;
144  for (;;) {
145  std::string::size_type pos2 = cfg.find_first_of(";=", pos);
146  if (pos2 == std::string::npos) {
147  cfgmap[cfg.substr(pos)] = "";
148  break;
149  }
150  if (cfg[pos2] == ';') {
151  cfgmap[cfg.substr(pos, pos2-pos)] = "";
152  } else {
153  std::string::size_type pos3 = pos2;
154  pos2 = cfg.find(";", pos2);
155  if (pos2 == std::string::npos) {
156  cfgmap[cfg.substr(pos, pos3-pos)] = cfg.substr(pos3 + 1);
157  break;
158  } else {
159  cfgmap[cfg.substr(pos, pos3-pos)] = cfg.substr(pos3 + 1, pos2 - pos3 - 1);
160  }
161  }
162  pos = pos2 + 1;
163  }
164  }
165 
166  if (cfgmap.find("__cplusplus") == cfgmap.end() && Preprocessor::cplusplus(settings,filename))
167  cfgmap["__cplusplus"] = "1";
168 
169  return cfgmap;
170 }
171 
172 
173 /** Just read the code into a string. Perform simple cleanup of the code */
174 std::string Preprocessor::read(std::istream &istr, const std::string &filename)
175 {
176  // The UTF-16 BOM is 0xfffe or 0xfeff.
177  unsigned int bom = 0;
178  if (istr.peek() >= 0xfe) {
179  bom = ((unsigned int)istr.get() << 8);
180  if (istr.peek() >= 0xfe)
181  bom |= (unsigned int)istr.get();
182  else
183  bom = 0; // allowed boms are 0/0xfffe/0xfeff
184  }
185 
186  if (_settings && _settings->terminated())
187  return "";
188 
190  return readpreprocessor(istr,bom);
191 
192  // ------------------------------------------------------------------------------------------
193  //
194  // handling <backslash><newline>
195  // when this is encountered the <backslash><newline> will be "skipped".
196  // on the next <newline>, extra newlines will be added
197  std::ostringstream code;
198  unsigned int newlines = 0;
199  for (unsigned char ch = readChar(istr,bom); istr.good(); ch = readChar(istr,bom)) {
200  // Replace assorted special chars with spaces..
201  if (((ch & 0x80) == 0) && (ch != '\n') && (std::isspace(ch) || std::iscntrl(ch)))
202  ch = ' ';
203 
204  // <backslash><newline>..
205  // for gcc-compatibility the trailing spaces should be ignored
206  // for vs-compatibility the trailing spaces should be kept
207  // See tickets #640 and #1869
208  // The solution for now is to have a compiler-dependent behaviour.
209  if (ch == '\\') {
210  unsigned char chNext;
211 
212  std::string spaces;
213 
214 #ifdef __GNUC__
215  // gcc-compatibility: ignore spaces
216  for (;; spaces += ' ') {
217  chNext = (unsigned char)istr.peek();
218  if (chNext != '\n' && chNext != '\r' &&
219  (std::isspace(chNext) || std::iscntrl(chNext))) {
220  // Skip whitespace between <backslash> and <newline>
221  (void)readChar(istr,bom);
222  continue;
223  }
224 
225  break;
226  }
227 #else
228  // keep spaces
229  chNext = (unsigned char)istr.peek();
230 #endif
231  if (chNext == '\n' || chNext == '\r') {
232  ++newlines;
233  (void)readChar(istr,bom); // Skip the "<backslash><newline>"
234  } else {
235  code << "\\" << spaces;
236  }
237  } else {
238  code << char(ch);
239 
240  // if there has been <backslash><newline> sequences, add extra newlines..
241  if (ch == '\n' && newlines > 0) {
242  code << std::string(newlines, '\n');
243  newlines = 0;
244  }
245  }
246  }
247  std::string result = code.str();
248  code.str("");
249 
250  // ------------------------------------------------------------------------------------------
251  //
252  // Remove all comments..
253  result = removeComments(result, filename);
254  if (_settings && _settings->terminated())
255  return "";
256 
257  // ------------------------------------------------------------------------------------------
258  //
259  // Clean up all preprocessor statements
260  result = preprocessCleanupDirectives(result);
261  if (_settings && _settings->terminated())
262  return "";
263 
264  // ------------------------------------------------------------------------------------------
265  //
266  // Clean up preprocessor #if statements with Parentheses
267  result = removeParentheses(result);
268  if (_settings && _settings->terminated())
269  return "";
270 
271  // Remove '#if 0' blocks
272  if (result.find("#if 0\n") != std::string::npos)
273  result = removeIf0(result);
274  if (_settings && _settings->terminated())
275  return "";
276 
277  return result;
278 }
279 
280 
281 /** read preprocessor statements */
282 std::string Preprocessor::readpreprocessor(std::istream &istr, const unsigned int bom)
283 {
284  enum { NEWLINE, SPACE, PREPROCESSOR, BACKSLASH, OTHER } state = NEWLINE;
285  std::ostringstream code;
286  unsigned int newlines = 1;
287  unsigned char chPrev = ' ';
288  for (unsigned char ch = readChar(istr,bom); istr.good(); ch = readChar(istr,bom)) {
289  // Replace assorted special chars with spaces..
290  if (((ch & 0x80) == 0) && (ch != '\n') && (std::isspace(ch) || std::iscntrl(ch)))
291  ch = ' ';
292 
293  if (ch == ' ' && chPrev == ' ')
294  continue;
295  if (state == PREPROCESSOR && chPrev == '/' && (ch == '/' || ch == '*'))
296  state = OTHER;
297  chPrev = ch;
298 
299  if (ch == '\n') {
300  if (state != BACKSLASH) {
301  state = NEWLINE;
302  code << std::string(newlines, '\n');
303  newlines = 1;
304  } else {
305  ++newlines;
306  state = PREPROCESSOR;
307  }
308  continue;
309  }
310 
311  switch (state) {
312  case NEWLINE:
313  if (ch==' ')
314  state = SPACE;
315  else if (ch == '#') {
316  state = PREPROCESSOR;
317  code << ch;
318  } else
319  state = OTHER;
320  break;
321  case SPACE:
322  if (ch == '#') {
323  state = PREPROCESSOR;
324  code << ch;
325  } else if (ch != ' ')
326  state = OTHER;
327  break;
328  case PREPROCESSOR:
329  code << ch;
330  if (ch == '\\')
331  state = BACKSLASH;
332  break;
333  case BACKSLASH:
334  code << ch;
335  if (ch != ' ')
336  state = PREPROCESSOR;
337  break;
338  case OTHER:
339  break;
340  };
341  }
342 
343  std::string result = preprocessCleanupDirectives(code.str());
344  result = removeParentheses(result);
345  return removeIf0(result);
346 }
347 
348 std::string Preprocessor::preprocessCleanupDirectives(const std::string &processedFile)
349 {
350  std::ostringstream code;
351  std::istringstream sstr(processedFile);
352 
353  std::string line;
354  while (std::getline(sstr, line)) {
355  // Trim lines..
356  if (!line.empty() && line[0] == ' ')
357  line.erase(0, line.find_first_not_of(" "));
358  if (!line.empty() && line[line.size()-1] == ' ')
359  line.erase(line.find_last_not_of(" ") + 1);
360 
361  // Preprocessor
362  if (!line.empty() && line[0] == '#') {
363  enum {
364  ESC_NONE,
365  ESC_SINGLE,
366  ESC_DOUBLE
367  } escapeStatus = ESC_NONE;
368 
369  char prev = ' '; // hack to make it skip spaces between # and the directive
370  code << "#";
371  std::string::const_iterator i = line.begin();
372  ++i;
373 
374  // need space.. #if( => #if (
375  bool needSpace = true;
376  while (i != line.end()) {
377  // disable esc-mode
378  if (escapeStatus != ESC_NONE) {
379  if (prev != '\\' && escapeStatus == ESC_SINGLE && *i == '\'') {
380  escapeStatus = ESC_NONE;
381  }
382  if (prev != '\\' && escapeStatus == ESC_DOUBLE && *i == '"') {
383  escapeStatus = ESC_NONE;
384  }
385  } else {
386  // enable esc-mode
387  if (escapeStatus == ESC_NONE && *i == '"')
388  escapeStatus = ESC_DOUBLE;
389  if (escapeStatus == ESC_NONE && *i == '\'')
390  escapeStatus = ESC_SINGLE;
391  }
392  // skip double whitespace between arguments
393  if (escapeStatus == ESC_NONE && prev == ' ' && *i == ' ') {
394  ++i;
395  continue;
396  }
397  // Convert #if( to "#if ("
398  if (escapeStatus == ESC_NONE) {
399  if (needSpace) {
400  if (*i == '(' || *i == '!')
401  code << " ";
402  else if (!std::isalpha((unsigned char)*i))
403  needSpace = false;
404  }
405  if (*i == '#')
406  needSpace = true;
407  }
408  code << *i;
409  if (escapeStatus != ESC_NONE && prev == '\\' && *i == '\\') {
410  prev = ' ';
411  } else {
412  prev = *i;
413  }
414  ++i;
415  }
416  if (escapeStatus != ESC_NONE) {
417  // unmatched quotes.. compiler should probably complain about this..
418  }
419  } else {
420  // Do not mess with regular code..
421  code << line;
422  }
423  code << (sstr.eof()?"":"\n");
424  }
425 
426  return code.str();
427 }
428 
429 static bool hasbom(const std::string &str)
430 {
431  return bool(str.size() >= 3 &&
432  static_cast<unsigned char>(str[0]) == 0xef &&
433  static_cast<unsigned char>(str[1]) == 0xbb &&
434  static_cast<unsigned char>(str[2]) == 0xbf);
435 }
436 
437 
438 // This wrapper exists because Sun's CC does not allow a static_cast
439 // from extern "C" int(*)(int) to int(*)(int).
440 static int tolowerWrapper(int c)
441 {
442  return std::tolower(c);
443 }
444 
445 
446 static bool isFallThroughComment(std::string comment)
447 {
448  // convert comment to lower case without whitespace
449  for (std::string::iterator i = comment.begin(); i != comment.end();) {
450  if (std::isspace(static_cast<unsigned char>(*i)))
451  i = comment.erase(i);
452  else
453  ++i;
454  }
455  std::transform(comment.begin(), comment.end(), comment.begin(), tolowerWrapper);
456 
457  return comment.find("fallthr") != std::string::npos ||
458  comment.find("fallsthr") != std::string::npos ||
459  comment.find("fall-thr") != std::string::npos ||
460  comment.find("dropthr") != std::string::npos ||
461  comment.find("passthr") != std::string::npos ||
462  comment.find("nobreak") != std::string::npos ||
463  comment == "fall";
464 }
465 
466 std::string Preprocessor::removeComments(const std::string &str, const std::string &filename)
467 {
468  // For the error report
469  unsigned int lineno = 1;
470 
471  // handling <backslash><newline>
472  // when this is encountered the <backslash><newline> will be "skipped".
473  // on the next <newline>, extra newlines will be added
474  unsigned int newlines = 0;
475  std::ostringstream code;
476  unsigned char previous = 0;
477  bool inPreprocessorLine = false;
478  std::vector<std::string> suppressionIDs;
479  bool fallThroughComment = false;
480 
481  for (std::string::size_type i = hasbom(str) ? 3U : 0U; i < str.length(); ++i) {
482  unsigned char ch = static_cast<unsigned char>(str[i]);
483  if (ch & 0x80) {
484  std::ostringstream errmsg;
485  errmsg << "The code contains characters that are unhandled. "
486  << "Neither unicode nor extended ASCII are supported. "
487  << "(line=" << lineno << ", character code=" << std::hex << (int(ch) & 0xff) << ")";
488  writeError(filename, lineno, _errorLogger, "syntaxError", errmsg.str());
489  _foundUnhandledChars = true;
490  }
491 
492  if (_settings && _settings->terminated())
493  return "";
494 
495  if (str.compare(i, 7, "#error ") == 0 || str.compare(i, 9, "#warning ") == 0) {
496  if (str.compare(i, 6, "#error") == 0)
497  code << "#error";
498 
499  i = str.find("\n", i);
500  if (i == std::string::npos)
501  break;
502 
503  --i;
504  continue;
505  }
506 
507  // First skip over any whitespace that may be present
508  if (std::isspace(ch)) {
509  if (ch == ' ' && previous == ' ') {
510  // Skip double white space
511  } else {
512  code << char(ch);
513  previous = ch;
514  }
515 
516  // if there has been <backslash><newline> sequences, add extra newlines..
517  if (ch == '\n') {
518  if (previous != '\\')
519  inPreprocessorLine = false;
520  ++lineno;
521  if (newlines > 0) {
522  code << std::string(newlines, '\n');
523  newlines = 0;
524  previous = '\n';
525  }
526  }
527 
528  continue;
529  }
530 
531  // Remove comments..
532  if (str.compare(i, 2, "//") == 0) {
533  std::size_t commentStart = i + 2;
534  i = str.find('\n', i);
535  if (i == std::string::npos)
536  break;
537  std::string comment(str, commentStart, i - commentStart);
538 
540  std::istringstream iss(comment);
541  std::string word;
542  iss >> word;
543  if (word == "cppcheck-suppress") {
544  iss >> word;
545  if (iss)
546  suppressionIDs.push_back(word);
547  }
548  }
549 
550  if (isFallThroughComment(comment)) {
551  fallThroughComment = true;
552  }
553 
554  code << "\n";
555  previous = '\n';
556  ++lineno;
557  } else if (str.compare(i, 2, "/*") == 0) {
558  std::size_t commentStart = i + 2;
559  unsigned char chPrev = 0;
560  ++i;
561  while (i < str.length() && (chPrev != '*' || ch != '/')) {
562  chPrev = ch;
563  ++i;
564  ch = static_cast<unsigned char>(str[i]);
565  if (ch == '\n') {
566  ++newlines;
567  ++lineno;
568  }
569  }
570  std::string comment(str, commentStart, i - commentStart - 1);
571 
572  if (isFallThroughComment(comment)) {
573  fallThroughComment = true;
574  }
575 
577  std::istringstream iss(comment);
578  std::string word;
579  iss >> word;
580  if (word == "cppcheck-suppress") {
581  iss >> word;
582  if (iss)
583  suppressionIDs.push_back(word);
584  }
585  }
586  } else if ((i == 0 || std::isspace((unsigned char)str[i-1])) && str.compare(i, 5, "__asm") == 0) {
587  while (i < str.size() && (std::isalpha((unsigned char)str[i]) || str[i] == '_'))
588  code << str[i++];
589  while (i < str.size() && std::isspace((unsigned char)str[i]))
590  code << str[i++];
591  if (str[i] == '{') {
592  // Ticket 4873: Extract comments from the __asm / __asm__'s content
593  std::string asmBody;
594  while (i < str.size() && str[i] != '}') {
595  if (str[i] == ';') {
596  std::string::size_type backslashN = str.find("\n", i);
597  if (backslashN != std::string::npos) // Ticket #4922: Don't go in infinite loop or crash if there is no '\n'
598  i = backslashN;
599  }
600  asmBody += str[i++];
601  }
602  code << removeComments(asmBody, filename);
603  code << '}';
604  } else
605  --i;
606  } else if (ch == '#' && previous == '\n') {
607  code << ch;
608  previous = ch;
609  inPreprocessorLine = true;
610 
611  // Add any pending inline suppressions that have accumulated.
612  if (!suppressionIDs.empty()) {
613  if (_settings != nullptr) {
614  // Add the suppressions.
615  for (std::size_t j = 0; j < suppressionIDs.size(); ++j) {
616  const std::string errmsg(_settings->nomsg.addSuppression(suppressionIDs[j], filename, lineno));
617  if (!errmsg.empty()) {
618  writeError(filename, lineno, _errorLogger, "cppcheckError", errmsg);
619  }
620  }
621  }
622  suppressionIDs.clear();
623  }
624  } else {
625  if (!inPreprocessorLine) {
626  // Not whitespace, not a comment, and not preprocessor.
627  // Must be code here!
628 
629  // First check for a "fall through" comment match, but only
630  // add a suppression if the next token is 'case' or 'default'
631  if (_settings && _settings->isEnabled("style") && _settings->experimental && fallThroughComment) {
632  std::string::size_type j = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz", i);
633  std::string tok = str.substr(i, j - i);
634  if (tok == "case" || tok == "default")
635  suppressionIDs.push_back("switchCaseFallThrough");
636  fallThroughComment = false;
637  }
638 
639  // Add any pending inline suppressions that have accumulated.
640  if (!suppressionIDs.empty()) {
641  if (_settings != nullptr) {
642  // Relative filename
643  std::string relativeFilename(filename);
644  if (_settings->_relativePaths) {
645  for (std::size_t j = 0U; j < _settings->_basePaths.size(); ++j) {
646  const std::string bp = _settings->_basePaths[j] + "/";
647  if (relativeFilename.compare(0,bp.size(),bp)==0) {
648  relativeFilename = relativeFilename.substr(bp.size());
649  }
650  }
651  }
652 
653  // Add the suppressions.
654  for (std::size_t j = 0; j < suppressionIDs.size(); ++j) {
655  const std::string errmsg(_settings->nomsg.addSuppression(suppressionIDs[j], relativeFilename, lineno));
656  if (!errmsg.empty()) {
657  writeError(filename, lineno, _errorLogger, "cppcheckError", errmsg);
658  }
659  }
660  }
661  suppressionIDs.clear();
662  }
663  }
664 
665  // String or char constants..
666  if (ch == '\"' || ch == '\'') {
667  code << char(ch);
668  char chNext;
669  do {
670  ++i;
671  chNext = str[i];
672  if (chNext == '\\') {
673  ++i;
674  char chSeq = str[i];
675  if (chSeq == '\n')
676  ++newlines;
677  else {
678  code << chNext;
679  code << chSeq;
680  previous = static_cast<unsigned char>(chSeq);
681  }
682  } else {
683  code << chNext;
684  previous = static_cast<unsigned char>(chNext);
685  }
686  } while (i < str.length() && chNext != ch && chNext != '\n');
687  }
688 
689  // Rawstring..
690  else if (str.compare(i,2,"R\"")==0) {
691  std::string delim;
692  for (std::string::size_type i2 = i+2; i2 < str.length(); ++i2) {
693  if (i2 > 16 ||
694  std::isspace(str[i2]) ||
695  std::iscntrl(str[i2]) ||
696  str[i2] == ')' ||
697  str[i2] == '\\') {
698  delim = " ";
699  break;
700  } else if (str[i2] == '(')
701  break;
702 
703  delim += str[i2];
704  }
705  const std::string::size_type endpos = str.find(")" + delim + "\"", i);
706  if (delim != " " && endpos != std::string::npos) {
707  unsigned int rawstringnewlines = 0;
708  code << '\"';
709  for (std::string::size_type p = i + 3 + delim.size(); p < endpos; ++p) {
710  if (str[p] == '\n') {
711  rawstringnewlines++;
712  code << "\\n";
713  } else if (std::iscntrl((unsigned char)str[p]) ||
714  std::isspace((unsigned char)str[p])) {
715  code << " ";
716  } else if (str[p] == '\"' || str[p] == '\'') {
717  code << "\\" << (char)str[p];
718  } else {
719  code << (char)str[p];
720  }
721  }
722  code << "\"";
723  if (rawstringnewlines > 0)
724  code << std::string(rawstringnewlines, '\n');
725  i = endpos + delim.size() + 1;
726  } else {
727  code << "R";
728  previous = 'R';
729  }
730  } else {
731  code << char(ch);
732  previous = ch;
733  }
734  }
735  }
736 
737  return code.str();
738 }
739 
740 std::string Preprocessor::removeIf0(const std::string &code)
741 {
742  std::ostringstream ret;
743  std::istringstream istr(code);
744  std::string line;
745  while (std::getline(istr,line)) {
746  ret << line << "\n";
747  if (line == "#if 0") {
748  // goto the end of the '#if 0' block
749  unsigned int level = 1;
750  bool in = false;
751  while (level > 0 && std::getline(istr,line)) {
752  if (line.compare(0,3,"#if") == 0)
753  ++level;
754  else if (line == "#endif")
755  --level;
756  else if ((line == "#else") || (line.compare(0, 5, "#elif") == 0)) {
757  if (level == 1)
758  in = true;
759  } else {
760  if (in)
761  ret << line << "\n";
762  else
763  // replace code within '#if 0' block with empty lines
764  ret << "\n";
765  continue;
766  }
767 
768  ret << line << "\n";
769  }
770  }
771  }
772  return ret.str();
773 }
774 
775 
776 std::string Preprocessor::removeParentheses(const std::string &str)
777 {
778  if (str.find("\n#if") == std::string::npos && str.compare(0, 3, "#if") != 0)
779  return str;
780 
781  std::istringstream istr(str);
782  std::ostringstream ret;
783  std::string line;
784  while (std::getline(istr, line)) {
785  if (line.compare(0, 3, "#if") == 0 || line.compare(0, 5, "#elif") == 0) {
786  std::string::size_type pos;
787  pos = 0;
788  while ((pos = line.find(" (", pos)) != std::string::npos)
789  line.erase(pos, 1);
790  pos = 0;
791  while ((pos = line.find("( ", pos)) != std::string::npos)
792  line.erase(pos + 1, 1);
793  pos = 0;
794  while ((pos = line.find(" )", pos)) != std::string::npos)
795  line.erase(pos, 1);
796  pos = 0;
797  while ((pos = line.find(") ", pos)) != std::string::npos)
798  line.erase(pos + 1, 1);
799 
800  // Remove inner parentheses "((..))"..
801  pos = 0;
802  while ((pos = line.find("((", pos)) != std::string::npos) {
803  ++pos;
804  std::string::size_type pos2 = line.find_first_of("()", pos + 1);
805  if (pos2 != std::string::npos && line[pos2] == ')') {
806  line.erase(pos2, 1);
807  line.erase(pos, 1);
808  }
809  }
810 
811  // "#if(A) => #if A", but avoid "#if (defined A) || defined (B)"
812  if ((line.compare(0, 4, "#if(") == 0 || line.compare(0, 6, "#elif(") == 0) &&
813  line[line.length() - 1] == ')') {
814  int ind = 0;
815  for (std::string::size_type i = 0; i < line.length(); ++i) {
816  if (line[i] == '(')
817  ++ind;
818  else if (line[i] == ')') {
819  --ind;
820  if (ind == 0) {
821  if (i == line.length() - 1) {
822  line[line.find('(')] = ' ';
823  line.erase(line.length() - 1);
824  }
825  break;
826  }
827  }
828  }
829  }
830 
831  if (line.compare(0, 4, "#if(") == 0)
832  line.insert(3, " ");
833  else if (line.compare(0, 6, "#elif(") == 0)
834  line.insert(5, " ");
835  }
836  ret << line << "\n";
837  }
838 
839  return ret.str();
840 }
841 
842 
843 void Preprocessor::removeAsm(std::string &str)
844 {
845  std::string::size_type pos = 0;
846  while ((pos = str.find("#asm\n", pos)) != std::string::npos) {
847  str.replace(pos, 4, "asm(");
848 
849  std::string::size_type pos2 = str.find("#endasm", pos);
850  if (pos2 != std::string::npos) {
851  str.replace(pos2, 7, ");");
852  pos = pos2;
853  }
854  }
855 }
856 
857 
858 void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::string> &result, const std::string &filename, const std::list<std::string> &includePaths)
859 {
860  std::list<std::string> configs;
861  std::string data;
862  preprocess(istr, data, configs, filename, includePaths);
863  for (std::list<std::string>::const_iterator it = configs.begin(); it != configs.end(); ++it) {
864  if (_settings && (_settings->userUndefs.find(*it) == _settings->userUndefs.end())) {
865  result[ *it ] = getcode(data, *it, filename);
866  }
867  }
868 }
869 
870 std::string Preprocessor::removeSpaceNearNL(const std::string &str)
871 {
872  std::string tmp;
873  char prev = 0;
874  for (std::size_t i = 0; i < str.size(); i++) {
875  if (str[i] == ' ' &&
876  ((i > 0 && prev == '\n') ||
877  (i + 1 < str.size() && str[i+1] == '\n')
878  )
879  ) {
880  // Ignore space that has new line in either side of it
881  } else {
882  tmp.append(1, str[i]);
883  prev = str[i];
884  }
885  }
886 
887  return tmp;
888 }
889 
890 std::string Preprocessor::replaceIfDefined(const std::string &str) const
891 {
892  std::string ret(str);
893  std::string::size_type pos;
894 
895  pos = 0;
896  while ((pos = ret.find("#if defined(", pos)) != std::string::npos) {
897  std::string::size_type pos2 = ret.find(")", pos + 9);
898  if (pos2 > ret.length() - 1)
899  break;
900  if (ret[pos2+1] == '\n') {
901  ret.erase(pos2, 1);
902  ret.erase(pos + 3, 9);
903  ret.insert(pos + 3, "def ");
904  }
905  ++pos;
906 
907  if (_settings && _settings->terminated())
908  return "";
909  }
910 
911  pos = 0;
912  while ((pos = ret.find("#if !defined(", pos)) != std::string::npos) {
913  std::string::size_type pos2 = ret.find(")", pos + 9);
914  if (pos2 > ret.length() - 1)
915  break;
916  if (ret[pos2+1] == '\n') {
917  ret.erase(pos2, 1);
918  ret.erase(pos + 3, 10);
919  ret.insert(pos + 3, "ndef ");
920  }
921  ++pos;
922 
923  if (_settings && _settings->terminated())
924  return "";
925  }
926 
927  pos = 0;
928  while ((pos = ret.find("#elif defined(", pos)) != std::string::npos) {
929  std::string::size_type pos2 = ret.find(")", pos + 9);
930  if (pos2 > ret.length() - 1)
931  break;
932  if (ret[pos2+1] == '\n') {
933  ret.erase(pos2, 1);
934  ret.erase(pos + 6, 8);
935  }
936  ++pos;
937 
938  if (_settings && _settings->terminated())
939  return "";
940  }
941 
942  return ret;
943 }
944 
945 void Preprocessor::preprocessWhitespaces(std::string &processedFile)
946 {
947  // Replace all tabs with spaces..
948  std::replace(processedFile.begin(), processedFile.end(), '\t', ' ');
949 
950  // Remove all indentation..
951  if (!processedFile.empty() && processedFile[0] == ' ')
952  processedFile.erase(0, processedFile.find_first_not_of(" "));
953 
954  // Remove space characters that are after or before new line character
955  processedFile = removeSpaceNearNL(processedFile);
956 }
957 
958 void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processedFile, std::list<std::string> &resultConfigurations, const std::string &filename, const std::list<std::string> &includePaths)
959 {
960  std::string forcedIncludes;
961 
962  if (file0.empty())
963  file0 = filename;
964 
965  processedFile = read(srcCodeStream, filename);
966 
967  if (_settings) {
968  for (std::list<std::string>::iterator it = _settings->userIncludes.begin();
969  it != _settings->userIncludes.end();
970  ++it) {
971  std::string cur = *it;
972 
973  // try to open file
974  std::ifstream fin;
975 
976  fin.open(cur.c_str());
977  if (!fin.is_open()) {
978  missingInclude(cur,
979  1,
980  cur,
981  UserHeader
982  );
983  continue;
984  }
985  const std::string fileData = read(fin, filename);
986 
987  fin.close();
988 
989  forcedIncludes =
990  forcedIncludes +
991  "#file \"" + cur + "\"\n" +
992  "#line 1\n" +
993  fileData + "\n" +
994  "#endfile\n"
995  ;
996  }
997 
998  for (std::vector<std::string>::iterator it = _settings->library.defines.begin();
999  it != _settings->library.defines.end();
1000  ++it) {
1001  forcedIncludes += *it;
1002  }
1003  }
1004 
1005  if (!forcedIncludes.empty()) {
1006  processedFile =
1007  forcedIncludes +
1008  "#file \"" + filename + "\"\n" +
1009  "#line 1\n" +
1010  processedFile +
1011  "#endfile\n"
1012  ;
1013  }
1014 
1015  // Remove asm(...)
1016  removeAsm(processedFile);
1017 
1018  // Replace "defined A" with "defined(A)"
1019  {
1020  std::istringstream istr(processedFile);
1021  std::ostringstream ostr;
1022  std::string line;
1023  while (std::getline(istr, line)) {
1024  if (line.compare(0, 4, "#if ") == 0 || line.compare(0, 6, "#elif ") == 0) {
1025  std::string::size_type pos = 0;
1026  while ((pos = line.find(" defined ")) != std::string::npos) {
1027  line[pos+8] = '(';
1028  pos = line.find_first_of(" |&", pos + 8);
1029  if (pos == std::string::npos)
1030  line += ")";
1031  else
1032  line.insert(pos, ")");
1033 
1034  if (_settings && _settings->terminated())
1035  return;
1036  }
1037  }
1038  ostr << line << "\n";
1039  }
1040  processedFile = ostr.str();
1041  }
1042 
1043  std::map<std::string, std::string> defs(getcfgmap(_settings ? _settings->userDefines : emptyString, _settings, filename));
1044 
1045  if (_settings && _settings->_maxConfigs == 1U) {
1046  std::set<std::string> pragmaOnce;
1047  std::list<std::string> includes;
1048  processedFile = handleIncludes(processedFile, filename, includePaths, defs, pragmaOnce, includes);
1049  resultConfigurations = getcfgs(processedFile, filename, defs);
1050  } else {
1051  handleIncludes(processedFile, filename, includePaths);
1052 
1053  processedFile = replaceIfDefined(processedFile);
1054 
1055  // Get all possible configurations..
1056  resultConfigurations = getcfgs(processedFile, filename, defs);
1057 
1058  // Remove configurations that are disabled by -U
1059  handleUndef(resultConfigurations);
1060  }
1061 }
1062 
1063 void Preprocessor::handleUndef(std::list<std::string> &configurations) const
1064 {
1065  if (_settings && !_settings->userUndefs.empty()) {
1066  for (std::list<std::string>::iterator cfg = configurations.begin(); cfg != configurations.end();) {
1067  bool undef = false;
1068  for (std::set<std::string>::const_iterator it = _settings->userUndefs.begin(); it != _settings->userUndefs.end(); ++it) {
1069  if (*it == *cfg)
1070  undef = true;
1071  else if (cfg->compare(0,it->length(),*it)==0 && cfg->find_first_of(";=") == it->length())
1072  undef = true;
1073  else if (cfg->find(";" + *it) == std::string::npos)
1074  ;
1075  else if (cfg->find(";" + *it + ";") != std::string::npos)
1076  undef = true;
1077  else if (cfg->find(";" + *it + "=") != std::string::npos)
1078  undef = true;
1079  else if (cfg->find(";" + *it) + it->size() + 1U == cfg->size())
1080  undef = true;
1081  }
1082 
1083  if (undef)
1084  configurations.erase(cfg++);
1085  else
1086  ++cfg;
1087  }
1088  }
1089 }
1090 
1091 // Get the DEF in this line: "#ifdef DEF"
1092 std::string Preprocessor::getdef(std::string line, bool def)
1093 {
1094  if (line.empty() || line[0] != '#')
1095  return "";
1096 
1097  // If def is true, the line must start with "#ifdef"
1098  if (def && line.compare(0, 7, "#ifdef ") != 0 && line.compare(0, 4, "#if ") != 0
1099  && (line.compare(0, 6, "#elif ") != 0 || line.compare(0, 7, "#elif !") == 0)) {
1100  return "";
1101  }
1102 
1103  // If def is false, the line must start with "#ifndef"
1104  if (!def && line.compare(0, 8, "#ifndef ") != 0 && line.compare(0, 7, "#elif !") != 0) {
1105  return "";
1106  }
1107 
1108  // Remove the "#ifdef" or "#ifndef"
1109  if (line.compare(0, 12, "#if defined ") == 0)
1110  line.erase(0, 11);
1111  else if (line.compare(0, 15, "#elif !defined(") == 0) {
1112  line.erase(0, 15);
1113  std::string::size_type pos = line.find(")");
1114  // if pos == ::npos then another part of the code will complain
1115  // about the mismatch
1116  if (pos != std::string::npos)
1117  line.erase(pos, 1);
1118  } else
1119  line.erase(0, line.find(" "));
1120 
1121  // Remove all spaces.
1122  std::string::size_type pos = 0;
1123  while ((pos = line.find(" ", pos)) != std::string::npos) {
1124  const unsigned char chprev(static_cast<unsigned char>((pos > 0) ? line[pos-1] : 0));
1125  const unsigned char chnext(static_cast<unsigned char>((pos + 1 < line.length()) ? line[pos+1] : 0));
1126  if ((std::isalnum(chprev) || chprev == '_') && (std::isalnum(chnext) || chnext == '_'))
1127  ++pos;
1128  else
1129  line.erase(pos, 1);
1130  }
1131 
1132  // The remaining string is our result.
1133  return line;
1134 }
1135 
1136 /** Simplify variable in variable map. */
1137 static Token *simplifyVarMapExpandValue(Token *tok, const std::map<std::string, std::string> &variables, std::set<std::string> seenVariables)
1138 {
1139  // TODO: handle function-macros too.
1140 
1141  // Prevent infinite recursion..
1142  if (seenVariables.find(tok->str()) != seenVariables.end())
1143  return tok;
1144  seenVariables.insert(tok->str());
1145 
1146  const std::map<std::string, std::string>::const_iterator it = variables.find(tok->str());
1147  if (it != variables.end()) {
1148  TokenList tokenList(nullptr);
1149  std::istringstream istr(it->second);
1150  if (tokenList.createTokens(istr)) {
1151  // expand token list
1152  for (Token *tok2 = tokenList.front(); tok2; tok2 = tok2->next()) {
1153  if (tok2->isName()) {
1154  tok2 = simplifyVarMapExpandValue(tok2, variables, seenVariables);
1155  }
1156  }
1157 
1158  // insert token list into "parent" token list
1159  for (const Token *tok2 = tokenList.front(); tok2; tok2 = tok2->next()) {
1160  if (tok2->previous()) {
1161  tok->insertToken(tok2->str());
1162  tok = tok->next();
1163  } else
1164  tok->str(tok2->str());
1165  }
1166  }
1167  }
1168 
1169  return tok;
1170 }
1171 
1172 /**
1173  * Simplifies the variable map. For example if the map contains A=>B, B=>1, then A=>B is simplified to A=>1.
1174  * @param [in,out] variables - a map of variable name to variable value. This map will be modified.
1175  */
1176 static void simplifyVarMap(std::map<std::string, std::string> &variables)
1177 {
1178  for (std::map<std::string, std::string>::iterator i = variables.begin(); i != variables.end(); ++i) {
1179  TokenList tokenList(nullptr);
1180  std::istringstream istr(i->second);
1181  if (tokenList.createTokens(istr)) {
1182  for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
1183  if (tok->isName()) {
1184  std::set<std::string> seenVariables;
1185  tok = simplifyVarMapExpandValue(tok, variables, seenVariables);
1186  }
1187  }
1188 
1189  std::string str;
1190  for (const Token *tok = tokenList.front(); tok; tok = tok->next())
1191  str.append((tok->previous() ? " " : "") + tok->str());
1192  i->second = str;
1193  }
1194  }
1195 }
1196 
1197 std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const std::string &filename, const std::map<std::string, std::string> &defs)
1198 {
1199  std::list<std::string> ret;
1200  ret.push_back("");
1201 
1202  std::list<std::string> deflist, ndeflist;
1203 
1204  // constants defined through "#define" in the code..
1205  std::set<std::string> defines;
1206  std::map<std::string, std::string> alldefinesmap(defs);
1207  std::stack<std::pair<std::string,bool> > includeStack;
1208  includeStack.push(std::pair<std::string,bool>(filename,false));
1209 
1210  // How deep into included files are we currently parsing?
1211  // 0=>Source file, 1=>Included by source file, 2=>included by header that was included by source file, etc
1212  int filelevel = 0;
1213 
1214  bool includeguard = false;
1215  unsigned int linenr = 0;
1216  std::istringstream istr(filedata);
1217  std::string line;
1218  while (std::getline(istr, line)) {
1219  ++linenr;
1220 
1221  if (_settings && _settings->terminated())
1222  return ret;
1223 
1224  if (_errorLogger)
1225  _errorLogger->reportProgress(filename, "Preprocessing (get configurations 1)", 0);
1226 
1227  if (line.empty())
1228  continue;
1229 
1230  if (line.compare(0, 6, "#file ") == 0) {
1231  includeguard = true;
1232  std::string::size_type start=line.find("\"");
1233  std::string::size_type end=line.find("\"",start+1);
1234  std::string includeFile=line.substr(start+1,end-start-1);
1235  bool fileExcluded = false;
1236  ++filelevel;
1237  if (! _settings) {
1238  fileExcluded = false;
1239  } else {
1240  fileExcluded = _settings->configurationExcluded(includeFile);
1241  }
1242  includeStack.push(std::pair<std::string,bool>(includeFile,fileExcluded));
1243  continue;
1244  }
1245 
1246  else if (line == "#endfile") {
1247  includeguard = false;
1248  includeStack.pop();
1249  if (filelevel > 0)
1250  --filelevel;
1251  continue;
1252  }
1253 
1254  if (line.compare(0, 8, "#define ") == 0) {
1255  bool valid = false;
1256  for (std::string::size_type pos = 8; pos < line.size(); ++pos) {
1257  char ch = line[pos];
1258  if (ch=='_' || (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (pos>8 && ch>='0' && ch<='9')) {
1259  valid = true;
1260  continue;
1261  }
1262  if (ch==' ' || ch=='(') {
1263  if (valid)
1264  break;
1265  }
1266  valid = false;
1267  break;
1268  }
1269  if (!valid)
1270  line.clear();
1271  else {
1272  std::string definestr = line.substr(8);
1273  const std::string::size_type spacepos = definestr.find(" ");
1274  if (spacepos != std::string::npos)
1275  definestr[spacepos] = '=';
1276  defines.insert(definestr);
1277 
1278  const std::string::size_type separatorpos = definestr.find_first_of("=(");
1279  if (separatorpos != std::string::npos && definestr[separatorpos] == '=') {
1280  const std::string varname(definestr.substr(0, separatorpos));
1281  const std::string value(definestr.substr(separatorpos + 1));
1282  alldefinesmap[varname] = value;
1283  }
1284  }
1285  }
1286 
1287  if (!line.empty() && line.compare(0, 3, "#if") != 0)
1288  includeguard = false;
1289 
1290  if (line.compare(0, 5, "#line") == 0)
1291  continue;
1292 
1293  if (line.empty() || line[0] != '#')
1294  continue;
1295 
1296  if (includeguard)
1297  continue;
1298 
1299  bool from_negation = false;
1300 
1301  std::string def = getdef(line, true);
1302  if (def.empty()) {
1303  def = getdef(line, false);
1304  // sub conditionals of ndef blocks need to be
1305  // constructed _without_ the negated define
1306  if (!def.empty())
1307  from_negation = true;
1308  }
1309  if (!def.empty()) {
1310  int par = 0;
1311  for (std::string::size_type pos = 0; pos < def.length(); ++pos) {
1312  if (def[pos] == '(')
1313  ++par;
1314  else if (def[pos] == ')') {
1315  --par;
1316  if (par < 0)
1317  break;
1318  }
1319  }
1320  if (par != 0) {
1321  std::ostringstream lineStream;
1322  lineStream << __LINE__;
1323 
1326  loc.setfile(filename);
1327  loc.line = linenr;
1328  errmsg._callStack.push_back(loc);
1329  errmsg._severity = Severity::fromString("error");
1330  errmsg.setmsg("mismatching number of '(' and ')' in this line: " + def);
1331  errmsg._id = "preprocessor" + lineStream.str();
1332  _errorLogger->reportErr(errmsg);
1333  ret.clear();
1334  return ret;
1335  }
1336 
1337  // Replace defined constants
1338  simplifyCondition(alldefinesmap, def, false);
1339 
1340  if (! deflist.empty() && line.compare(0, 6, "#elif ") == 0)
1341  deflist.pop_back();
1342 
1343  // translate A==1 condition to A=1 configuration
1344  if (def.find("==") != std::string::npos) {
1345  // Check if condition match pattern "%var% == %num%"
1346  // %var%
1347  std::string::size_type pos = 0;
1348  if (std::isalpha((unsigned char)def[pos]) || def[pos] == '_') {
1349  ++pos;
1350  while (std::isalnum((unsigned char)def[pos]) || def[pos] == '_')
1351  ++pos;
1352  }
1353 
1354  // ==
1355  if (def.compare(pos,2,"==")==0)
1356  pos += 2;
1357 
1358  // %num%
1359  if (pos<def.size() && std::isdigit(def[pos])) {
1360  if (def.compare(pos,2,"0x")==0) {
1361  pos += 2;
1362  if (pos >= def.size())
1363  pos = 0;
1364  while (pos < def.size() && std::isxdigit((unsigned char)def[pos]))
1365  ++pos;
1366  } else {
1367  while (pos < def.size() && std::isdigit((unsigned char)def[pos]))
1368  ++pos;
1369  }
1370 
1371  // Does the condition match the pattern "%var% == %num%"?
1372  if (pos == def.size()) {
1373  def.erase(def.find("=="),1);
1374  }
1375  }
1376  }
1377 
1378  deflist.push_back(def);
1379  def = "";
1380 
1381  for (std::list<std::string>::const_iterator it = deflist.begin(); it != deflist.end(); ++it) {
1382  if (*it == "0")
1383  break;
1384  if (*it == "1" || *it == "!")
1385  continue;
1386 
1387  // don't add "T;T":
1388  // treat two and more similar nested conditions as one
1389  if (def != *it) {
1390  if (! def.empty())
1391  def += ";";
1392  def += *it;
1393  }
1394 
1395  /* TODO: Fix TestPreprocessor::test7e (#2552)
1396  else
1397  {
1398  std::ostringstream lineStream;
1399  lineStream << __LINE__;
1400 
1401  ErrorLogger::ErrorMessage errmsg;
1402  ErrorLogger::ErrorMessage::FileLocation loc;
1403  loc.setfile(filename);
1404  loc.line = linenr;
1405  errmsg._callStack.push_back(loc);
1406  errmsg._severity = Severity::error;
1407  errmsg.setmsg(*it+" is already guaranteed to be defined");
1408  errmsg._id = "preprocessor" + lineStream.str();
1409  _errorLogger->reportErr(errmsg);
1410  }
1411  */
1412  }
1413  if (from_negation) {
1414  ndeflist.push_back(deflist.back());
1415  deflist.back() = "!";
1416  }
1417 
1418  if (std::find(ret.begin(), ret.end(), def) == ret.end()) {
1419  if (!includeStack.top().second) {
1420  ret.push_back(def);
1421  } else {
1423  std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
1424  const ErrorLogger::ErrorMessage errmsg(locationList, Severity::debug,
1425  "Configuration not considered: " + def +" for file:"+includeStack.top().first, "debug", false);
1426  _errorLogger->reportErr(errmsg);
1427  }
1428  }
1429  }
1430  }
1431 
1432  else if (line.compare(0, 5, "#else") == 0 && ! deflist.empty()) {
1433  if (deflist.back() == "!" && !ndeflist.empty()) {
1434  deflist.back() = ndeflist.back();
1435  ndeflist.pop_back();
1436  } else {
1437  std::string tempDef((deflist.back() == "1") ? "0" : "1");
1438  deflist.back() = tempDef;
1439  }
1440  }
1441 
1442  else if (line.compare(0, 6, "#endif") == 0 && ! deflist.empty()) {
1443  if (deflist.back() == "!" && !ndeflist.empty())
1444  ndeflist.pop_back();
1445  deflist.pop_back();
1446  }
1447  }
1448 
1449  // Remove defined constants from ifdef configurations..
1450  std::size_t count = 0;
1451  for (std::list<std::string>::iterator it = ret.begin(); it != ret.end(); ++it) {
1452  if (_errorLogger)
1453  _errorLogger->reportProgress(filename, "Preprocessing (get configurations 2)", (100 * count++) / ret.size());
1454 
1455  std::string cfg(*it);
1456  for (std::set<std::string>::const_iterator it2 = defines.begin(); it2 != defines.end(); ++it2) {
1457  std::string::size_type pos = 0;
1458 
1459  // Get name of define
1460  std::string defineName(*it2);
1461  if (defineName.find_first_of("=(") != std::string::npos)
1462  defineName.erase(defineName.find_first_of("=("));
1463 
1464  // Remove ifdef configurations that match the defineName
1465  while ((pos = cfg.find(defineName, pos)) != std::string::npos) {
1466  const std::string::size_type pos1 = pos;
1467  ++pos;
1468  if (pos1 > 0 && cfg[pos1-1] != ';')
1469  continue;
1470  const std::string::size_type pos2 = pos1 + defineName.length();
1471  if (pos2 < cfg.length() && cfg[pos2] != ';')
1472  continue;
1473  --pos;
1474  cfg.erase(pos, defineName.length());
1475  }
1476  }
1477  if (cfg.length() != it->length()) {
1478  while (cfg.length() > 0 && cfg[0] == ';')
1479  cfg.erase(0, 1);
1480 
1481  while (cfg.length() > 0 && cfg[cfg.length()-1] == ';')
1482  cfg.erase(cfg.length() - 1);
1483 
1484  std::string::size_type pos = 0;
1485  while ((pos = cfg.find(";;", pos)) != std::string::npos)
1486  cfg.erase(pos, 1);
1487 
1488  *it = cfg;
1489  }
1490  }
1491 
1492  // convert configurations: "defined(A) && defined(B)" => "A;B"
1493  for (std::list<std::string>::iterator it = ret.begin(); it != ret.end(); ++it) {
1494  std::string s(*it);
1495 
1496  if (s.find("&&") != std::string::npos) {
1497  Tokenizer tokenizer(_settings, _errorLogger);
1498  if (!tokenizer.tokenizeCondition(s)) {
1499  std::ostringstream lineStream;
1500  lineStream << __LINE__;
1501 
1504  loc.setfile(filename);
1505  loc.line = 1;
1506  errmsg._callStack.push_back(loc);
1507  errmsg._severity = Severity::error;
1508  errmsg.setmsg("Error parsing this: " + s);
1509  errmsg._id = "preprocessor" + lineStream.str();
1510  _errorLogger->reportErr(errmsg);
1511  }
1512 
1513 
1514  const Token *tok = tokenizer.tokens();
1515  std::set<std::string> varList;
1516  while (tok) {
1517  if (Token::Match(tok, "defined ( %var% )")) {
1518  varList.insert(tok->strAt(2));
1519  tok = tok->tokAt(4);
1520  if (tok && tok->str() == "&&") {
1521  tok = tok->next();
1522  }
1523  } else if (Token::Match(tok, "%var% ;")) {
1524  varList.insert(tok->str());
1525  tok = tok->tokAt(2);
1526  } else {
1527  break;
1528  }
1529  }
1530 
1531  s = join(varList, ';');
1532 
1533  if (!s.empty())
1534  *it = s;
1535  }
1536  }
1537 
1538  // Convert configurations into a canonical form: B;C;A or C;A;B => A;B;C
1539  for (std::list<std::string>::iterator it = ret.begin(); it != ret.end(); ++it)
1540  *it = unify(*it, ';');
1541 
1542  // Remove duplicates from the ret list..
1543  ret.sort();
1544  ret.unique();
1545 
1546  // cleanup unhandled configurations..
1547  for (std::list<std::string>::iterator it = ret.begin(); it != ret.end();) {
1548  const std::string s(*it + ";");
1549 
1550  bool unhandled = false;
1551 
1552  for (std::string::size_type pos = 0; pos < s.length(); ++pos) {
1553  const unsigned char c = static_cast<unsigned char>(s[pos]);
1554 
1555  // ok with ";"
1556  if (c == ';')
1557  continue;
1558 
1559  // identifier..
1560  if (std::isalpha(c) || c == '_') {
1561  while (std::isalnum((unsigned char)s[pos]) || s[pos] == '_')
1562  ++pos;
1563  if (s[pos] == '=') {
1564  ++pos;
1565  while (std::isdigit((unsigned char)s[pos]))
1566  ++pos;
1567  if (s[pos] != ';') {
1568  unhandled = true;
1569  break;
1570  }
1571  }
1572 
1573  --pos;
1574  continue;
1575  }
1576 
1577  // not ok..
1578  else {
1579  unhandled = true;
1580  break;
1581  }
1582  }
1583 
1584  if (unhandled) {
1585  // unhandled ifdef configuration..
1587  std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
1588  const ErrorLogger::ErrorMessage errmsg(locationList, Severity::debug, "unhandled configuration: " + *it, "debug", false);
1589  _errorLogger->reportErr(errmsg);
1590  }
1591 
1592  ret.erase(it++);
1593  } else {
1594  ++it;
1595  }
1596  }
1597 
1598  return ret;
1599 }
1600 
1601 
1602 void Preprocessor::simplifyCondition(const std::map<std::string, std::string> &cfg, std::string &condition, bool match)
1603 {
1604  const Settings settings;
1605  Tokenizer tokenizer(&settings, _errorLogger);
1606  if (!tokenizer.tokenizeCondition("(" + condition + ")")) {
1607  // If tokenize returns false, then there is syntax error in the
1608  // code which we can't handle. So stop here.
1609  return;
1610  }
1611 
1612  if (Token::Match(tokenizer.tokens(), "( %var% )")) {
1613  std::map<std::string,std::string>::const_iterator var = cfg.find(tokenizer.tokens()->strAt(1));
1614  if (var != cfg.end()) {
1615  const std::string &value = (*var).second;
1616  condition = (value == "0") ? "0" : "1";
1617  } else if (match)
1618  condition = "0";
1619  return;
1620  }
1621 
1622  if (Token::Match(tokenizer.tokens(), "( ! %var% )")) {
1623  std::map<std::string,std::string>::const_iterator var = cfg.find(tokenizer.tokens()->strAt(2));
1624 
1625  if (var == cfg.end())
1626  condition = "1";
1627  else if (var->second == "0")
1628  condition = "1";
1629  else if (match)
1630  condition = "0";
1631  return;
1632  }
1633 
1634  // replace variable names with values..
1635  for (Token *tok = const_cast<Token *>(tokenizer.tokens()); tok; tok = tok->next()) {
1636  if (!tok->isName())
1637  continue;
1638 
1639  if (Token::Match(tok, "defined ( %var% )")) {
1640  if (cfg.find(tok->strAt(2)) != cfg.end())
1641  tok->str("1");
1642  else if (match)
1643  tok->str("0");
1644  else
1645  continue;
1646  tok->deleteNext(3);
1647  continue;
1648  }
1649 
1650  if (Token::Match(tok, "defined %var%")) {
1651  if (cfg.find(tok->strAt(1)) != cfg.end())
1652  tok->str("1");
1653  else if (match)
1654  tok->str("0");
1655  else
1656  continue;
1657  tok->deleteNext();
1658  continue;
1659  }
1660 
1661  const std::map<std::string, std::string>::const_iterator it = cfg.find(tok->str());
1662  if (it != cfg.end()) {
1663  if (!it->second.empty()) {
1664  // Tokenize the value
1665  Tokenizer tokenizer2(&settings, _errorLogger);
1666  tokenizer2.tokenizeCondition(it->second);
1667 
1668  // Copy the value tokens
1669  std::stack<Token *> link;
1670  for (const Token *tok2 = tokenizer2.tokens(); tok2; tok2 = tok2->next()) {
1671  tok->str(tok2->str());
1672 
1673  if (Token::Match(tok2,"[{([]"))
1674  link.push(tok);
1675  else if (!link.empty() && Token::Match(tok2,"[})]]")) {
1676  Token::createMutualLinks(link.top(), tok);
1677  link.pop();
1678  }
1679 
1680  if (tok2->next()) {
1681  tok->insertToken("");
1682  tok = tok->next();
1683  }
1684  }
1685  } else if ((!tok->previous() || Token::Match(tok->previous(), "&&|%oror%|(")) &&
1686  (!tok->next() || Token::Match(tok->next(), "&&|%oror%|)")))
1687  tok->str("1");
1688  else
1689  tok->deleteThis();
1690  }
1691  }
1692 
1693  // simplify calculations..
1695  bool modified = true;
1696  while (modified) {
1697  modified = false;
1698  modified |= tokenizer.simplifySizeof();
1699  modified |= tokenizer.simplifyCalculations();
1700  modified |= tokenizer.simplifyConstTernaryOp();
1701  modified |= tokenizer.simplifyRedundantParentheses();
1702  for (Token *tok = const_cast<Token *>(tokenizer.tokens()); tok; tok = tok->next()) {
1703  if (Token::Match(tok, "! %num%")) {
1704  tok->deleteThis();
1705  tok->str(tok->str() == "0" ? "1" : "0");
1706  modified = true;
1707  }
1708  }
1709  }
1710 
1711  for (Token *tok = const_cast<Token *>(tokenizer.tokens()); tok; tok = tok->next()) {
1712  if (Token::Match(tok, "(|%oror%|&& %num% &&|%oror%|)")) {
1713  if (tok->next()->str() != "0") {
1714  tok->next()->str("1");
1715  }
1716  }
1717  }
1718 
1719  for (Token *tok = const_cast<Token *>(tokenizer.tokens()); tok; tok = tok->next()) {
1720  while (Token::Match(tok, "(|%oror% %any% %oror% 1")) {
1721  tok->deleteNext(2);
1722  if (tok->tokAt(-3))
1723  tok = tok->tokAt(-3);
1724  }
1725  }
1726 
1727  if (Token::simpleMatch(tokenizer.tokens(), "( 1 )") ||
1728  Token::simpleMatch(tokenizer.tokens(), "( 1 ||"))
1729  condition = "1";
1730  else if (Token::simpleMatch(tokenizer.tokens(), "( 0 )"))
1731  condition = "0";
1732 }
1733 
1734 bool Preprocessor::match_cfg_def(std::map<std::string, std::string> cfg, std::string def)
1735 {
1736  /*
1737  std::cout << "cfg: \"";
1738  for (std::map<std::string, std::string>::const_iterator it = cfg.begin(); it != cfg.end(); ++it)
1739  {
1740  std::cout << it->first;
1741  if (!it->second.empty())
1742  std::cout << "=" << it->second;
1743  std::cout << ";";
1744  }
1745  std::cout << "\" ";
1746  std::cout << "def: \"" << def << "\"\n";
1747  */
1748 
1749  simplifyVarMap(cfg);
1750  simplifyCondition(cfg, def, true);
1751 
1752  if (cfg.find(def) != cfg.end())
1753  return true;
1754 
1755  if (def == "0")
1756  return false;
1757 
1758  if (def == "1")
1759  return true;
1760 
1761  return false;
1762 }
1763 
1764 std::string Preprocessor::getcode(const std::string &filedata, const std::string &cfg, const std::string &filename)
1765 {
1766  // For the error report
1767  unsigned int lineno = 0;
1768 
1769  std::ostringstream ret;
1770 
1771  bool match = true;
1772  std::list<bool> matching_ifdef;
1773  std::list<bool> matched_ifdef;
1774 
1775  // Create a map for the cfg for faster access to defines
1776  std::map<std::string, std::string> cfgmap(getcfgmap(cfg, _settings, filename));
1777 
1778  std::stack<std::string> filenames;
1779  filenames.push(filename);
1780  std::stack<unsigned int> lineNumbers;
1781  std::istringstream istr(filedata);
1782  std::string line;
1783  while (std::getline(istr, line)) {
1784  ++lineno;
1785 
1786  if (_settings && _settings->terminated())
1787  return "";
1788 
1789  if (line.compare(0, 11, "#pragma asm") == 0) {
1790  ret << "\n";
1791  bool found_end = false;
1792  while (getline(istr, line)) {
1793  if (line.compare(0, 14, "#pragma endasm") == 0) {
1794  found_end = true;
1795  break;
1796  }
1797 
1798  ret << "\n";
1799  }
1800  if (!found_end)
1801  break;
1802 
1803  if (line.find("=") != std::string::npos) {
1804  Tokenizer tokenizer(_settings, _errorLogger);
1805  line.erase(0, sizeof("#pragma endasm"));
1806  std::istringstream tempIstr(line);
1807  tokenizer.tokenize(tempIstr, "", "", true);
1808  if (Token::Match(tokenizer.tokens(), "( %var% = %any% )")) {
1809  ret << "asm(" << tokenizer.tokens()->strAt(1) << ");";
1810  }
1811  }
1812 
1813  ret << "\n";
1814 
1815  continue;
1816  }
1817 
1818  const std::string def = getdef(line, true);
1819  const std::string ndef = getdef(line, false);
1820 
1821  const bool emptymatch = matching_ifdef.empty() || matched_ifdef.empty();
1822 
1823  if (line.compare(0, 8, "#define ") == 0) {
1824  match = true;
1825 
1826  if (_settings) {
1827  typedef std::set<std::string>::const_iterator It;
1828  for (It it = _settings->userUndefs.begin(); it != _settings->userUndefs.end(); ++it) {
1829  std::string::size_type pos = line.find_first_not_of(' ',8);
1830  if (pos != std::string::npos) {
1831  std::string::size_type pos2 = line.find(*it,pos);
1832  if ((pos2 != std::string::npos) &&
1833  ((line.size() == pos2 + (*it).size()) ||
1834  (line[pos2 + (*it).size()] == ' ') ||
1835  (line[pos2 + (*it).size()] == '('))) {
1836  match = false;
1837  break;
1838  }
1839  }
1840  }
1841  }
1842 
1843  if (match) {
1844  for (std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it) {
1845  if (!bool(*it)) {
1846  match = false;
1847  break;
1848  }
1849  }
1850  }
1851 
1852  if (match) {
1853  std::string::size_type pos = line.find_first_of(" (", 8);
1854  if (pos == std::string::npos)
1855  cfgmap[line.substr(8)] = "";
1856  else if (line[pos] == ' ') {
1857  std::string value(line.substr(pos + 1));
1858  if (cfgmap.find(value) != cfgmap.end())
1859  value = cfgmap[value];
1860  cfgmap[line.substr(8, pos - 8)] = value;
1861  } else
1862  cfgmap[line.substr(8, pos - 8)] = "";
1863  }
1864  }
1865 
1866  else if (line.compare(0, 7, "#undef ") == 0) {
1867  const std::string name(line.substr(7));
1868  cfgmap.erase(name);
1869  }
1870 
1871  else if (!emptymatch && line.compare(0, 7, "#elif !") == 0) {
1872  if (matched_ifdef.back()) {
1873  matching_ifdef.back() = false;
1874  } else {
1875  if (!match_cfg_def(cfgmap, ndef)) {
1876  matching_ifdef.back() = true;
1877  matched_ifdef.back() = true;
1878  }
1879  }
1880  }
1881 
1882  else if (!emptymatch && line.compare(0, 6, "#elif ") == 0) {
1883  if (matched_ifdef.back()) {
1884  matching_ifdef.back() = false;
1885  } else {
1886  if (match_cfg_def(cfgmap, def)) {
1887  matching_ifdef.back() = true;
1888  matched_ifdef.back() = true;
1889  }
1890  }
1891  }
1892 
1893  else if (line.compare(0,4,"#if ") == 0) {
1894  matching_ifdef.push_back(match_cfg_def(cfgmap, line.substr(4)));
1895  matched_ifdef.push_back(matching_ifdef.back());
1896  }
1897 
1898  else if (! def.empty()) {
1899  matching_ifdef.push_back(cfgmap.find(def) != cfgmap.end());
1900  matched_ifdef.push_back(matching_ifdef.back());
1901  }
1902 
1903  else if (! ndef.empty()) {
1904  matching_ifdef.push_back(cfgmap.find(ndef) == cfgmap.end());
1905  matched_ifdef.push_back(matching_ifdef.back());
1906  }
1907 
1908  else if (!emptymatch && line == "#else") {
1909  if (! matched_ifdef.empty())
1910  matching_ifdef.back() = ! matched_ifdef.back();
1911  }
1912 
1913  else if (line.compare(0, 6, "#endif") == 0) {
1914  if (! matched_ifdef.empty())
1915  matched_ifdef.pop_back();
1916  if (! matching_ifdef.empty())
1917  matching_ifdef.pop_back();
1918  }
1919 
1920  if (!line.empty() && line[0] == '#') {
1921  match = true;
1922  for (std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it) {
1923  if (!bool(*it)) {
1924  match = false;
1925  break;
1926  }
1927  }
1928  }
1929 
1930  // #error => return ""
1931  if (match && line.compare(0, 6, "#error") == 0) {
1932  if (_settings && !_settings->userDefines.empty() && !_settings->_force) {
1933  error(filenames.top(), lineno, line);
1934  }
1935  return "";
1936  }
1937 
1938  if (!match && (line.compare(0, 8, "#define ") == 0 ||
1939  line.compare(0, 6, "#undef") == 0)) {
1940  // Remove define that is not part of this configuration
1941  line = "";
1942  } else if (line.compare(0, 7, "#file \"") == 0 ||
1943  line.compare(0, 8, "#endfile") == 0 ||
1944  line.compare(0, 8, "#define ") == 0 ||
1945  line.compare(0, 6, "#line ") == 0 ||
1946  line.compare(0, 6, "#undef") == 0) {
1947  // We must not remove #file tags or line numbers
1948  // are corrupted. File tags are removed by the tokenizer.
1949 
1950  // Keep location info updated
1951  if (line.compare(0, 7, "#file \"") == 0) {
1952  filenames.push(line.substr(7, line.size() - 8));
1953  lineNumbers.push(lineno);
1954  lineno = 0;
1955  } else if (line.compare(0, 8, "#endfile") == 0) {
1956  if (filenames.size() > 1U)
1957  filenames.pop();
1958 
1959  if (!lineNumbers.empty()) {
1960  lineno = lineNumbers.top();
1961  lineNumbers.pop();
1962  }
1963  }
1964  } else if (!match || line.compare(0, 1, "#") == 0) {
1965  // Remove #if, #else, #pragma etc, leaving only
1966  // #define, #undef, #file and #endfile. and also lines
1967  // which are not part of this configuration.
1968  line = "";
1969  }
1970 
1971  ret << line << "\n";
1972  }
1973 
1974  if (!validateCfg(ret.str(), cfg)) {
1975  return "";
1976  }
1977 
1978  return expandMacros(ret.str(), filename, cfg, _errorLogger);
1979 }
1980 
1981 void Preprocessor::error(const std::string &filename, unsigned int linenr, const std::string &msg)
1982 {
1983  std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
1984  if (!filename.empty()) {
1986  loc.line = linenr;
1987  loc.setfile(filename);
1988  locationList.push_back(loc);
1989  }
1992  msg,
1993  "preprocessorErrorDirective",
1994  false));
1995 }
1996 
1998 {
1999  std::string result;
2000  std::string::size_type i = str.find_first_of("<\"");
2001  if (i == std::string::npos) {
2002  str = "";
2003  return NoHeader;
2004  }
2005 
2006  char c = str[i];
2007  if (c == '<')
2008  c = '>';
2009 
2010  for (i = i + 1; i < str.length(); ++i) {
2011  if (str[i] == c)
2012  break;
2013 
2014  result.append(1, str[i]);
2015  }
2016 
2017  // Linux can't open include paths with \ separator, so fix them
2018  std::replace(result.begin(), result.end(), '\\', '/');
2019 
2020  str = result;
2021 
2022  return (c == '\"') ? UserHeader : SystemHeader;
2023 }
2024 
2025 /**
2026  * Try to open header
2027  * @param filename header name (in/out)
2028  * @param includePaths paths where to look for the file
2029  * @param filePath path to the header file
2030  * @param fin file input stream (in/out)
2031  * @return if file is opened then true is returned
2032  */
2033 static bool openHeader(std::string &filename, const std::list<std::string> &includePaths, const std::string &filePath, std::ifstream &fin)
2034 {
2035  fin.open((filePath + filename).c_str());
2036  if (fin.is_open()) {
2037  filename = filePath + filename;
2038  return true;
2039  }
2040 
2041  std::list<std::string> includePaths2(includePaths);
2042  includePaths2.push_front("");
2043 
2044  for (std::list<std::string>::const_iterator iter = includePaths2.begin(); iter != includePaths2.end(); ++iter) {
2045  const std::string nativePath(Path::toNativeSeparators(*iter));
2046  fin.open((nativePath + filename).c_str());
2047  if (fin.is_open()) {
2048  filename = nativePath + filename;
2049  return true;
2050  }
2051  fin.clear();
2052  }
2053 
2054  return false;
2055 }
2056 
2057 
2058 std::string Preprocessor::handleIncludes(const std::string &code, const std::string &filePath, const std::list<std::string> &includePaths, std::map<std::string,std::string> &defs, std::set<std::string> &pragmaOnce, std::list<std::string> includes)
2059 {
2060  const std::string path(filePath.substr(0, 1 + filePath.find_last_of("\\/")));
2061 
2062  // current #if indent level.
2063  std::stack<bool>::size_type indent = 0;
2064 
2065  // how deep does the #if match? this can never be bigger than "indent".
2066  std::stack<bool>::size_type indentmatch = 0;
2067 
2068  // has there been a true #if condition at the current indentmatch level?
2069  // then no more #elif or #else can be true before the #endif is seen.
2070  std::stack<bool> elseIsTrueStack;
2071 
2072  unsigned int linenr = 0;
2073 
2074  std::set<std::string> undefs = _settings ? _settings->userUndefs : std::set<std::string>();
2075 
2076  if (_errorLogger)
2077  _errorLogger->reportProgress(filePath, "Preprocessor (handleIncludes)", 0);
2078 
2079  std::ostringstream ostr;
2080  std::istringstream istr(code);
2081  std::string line;
2082  bool suppressCurrentCodePath = false;
2083  while (std::getline(istr,line)) {
2084  ++linenr;
2085 
2086  if (_settings && _settings->terminated())
2087  return "";
2088 
2089  // has there been a true #if condition at the current indentmatch level?
2090  // then no more #elif or #else can be true before the #endif is seen.
2091  while (elseIsTrueStack.size() != indentmatch + 1) {
2092  if (elseIsTrueStack.size() < indentmatch + 1) {
2093  elseIsTrueStack.push(true);
2094  } else {
2095  elseIsTrueStack.pop();
2096  }
2097  }
2098 
2099  if (elseIsTrueStack.empty()) {
2100  writeError(filePath, linenr, _errorLogger, "syntaxError", "Syntax error in preprocessor code");
2101  return "";
2102  }
2103 
2104  std::stack<bool>::reference elseIsTrue = elseIsTrueStack.top();
2105 
2106  if (line == "#pragma once") {
2107  pragmaOnce.insert(filePath);
2108  } else if (line.compare(0,7,"#ifdef ") == 0) {
2109  if (indent == indentmatch) {
2110  const std::string tag = getdef(line,true);
2111  if (defs.find(tag) != defs.end()) {
2112  elseIsTrue = false;
2113  indentmatch++;
2114  } else if (undefs.find(tag) != undefs.end()) {
2115  elseIsTrue = true;
2116  indentmatch++;
2117  suppressCurrentCodePath = true;
2118  }
2119  }
2120  ++indent;
2121 
2122  if (indent == indentmatch + 1)
2123  elseIsTrue = true;
2124  } else if (line.compare(0,8,"#ifndef ") == 0) {
2125  if (indent == indentmatch) {
2126  const std::string tag = getdef(line,false);
2127  if (defs.find(tag) == defs.end()) {
2128  elseIsTrue = false;
2129  indentmatch++;
2130  } else if (undefs.find(tag) != undefs.end()) {
2131  elseIsTrue = false;
2132  indentmatch++;
2133  suppressCurrentCodePath = false;
2134  }
2135  }
2136  ++indent;
2137 
2138  if (indent == indentmatch + 1)
2139  elseIsTrue = true;
2140 
2141  } else if (line.compare(0,4,"#if ") == 0) {
2142  if (!suppressCurrentCodePath && indent == indentmatch && match_cfg_def(defs, line.substr(4))) {
2143  elseIsTrue = false;
2144  indentmatch++;
2145  }
2146  ++indent;
2147 
2148  if (indent == indentmatch + 1)
2149  elseIsTrue = true; // this value doesn't matter when suppressCurrentCodePath is true
2150  } else if (line.compare(0,6,"#elif ") == 0 || line.compare(0,5,"#else") == 0) {
2151  if (!elseIsTrue) {
2152  if (indentmatch == indent) {
2153  indentmatch = indent - 1;
2154  }
2155  } else {
2156  if (indentmatch == indent) {
2157  indentmatch = indent - 1;
2158  } else if (indentmatch == indent - 1) {
2159  if (line.compare(0,5,"#else")==0 || match_cfg_def(defs,line.substr(6))) {
2160  indentmatch = indent;
2161  elseIsTrue = false;
2162  }
2163  }
2164  }
2165  } else if (line.compare(0, 6, "#endif") == 0) {
2166  if (indent > 0)
2167  --indent;
2168  if (indentmatch > indent || indent == 0) {
2169  indentmatch = indent;
2170  elseIsTrue = false;
2171  suppressCurrentCodePath = false;
2172  }
2173  } else if (indentmatch == indent) {
2174  if (!suppressCurrentCodePath && line.compare(0, 8, "#define ") == 0) {
2175  const unsigned int endOfDefine = 8;
2176  std::string::size_type endOfTag = line.find_first_of("( ", endOfDefine);
2177  std::string tag;
2178 
2179  // define a symbol
2180  if (endOfTag == std::string::npos) {
2181  tag = line.substr(endOfDefine);
2182  defs[tag] = "";
2183  } else {
2184  tag = line.substr(endOfDefine, endOfTag-endOfDefine);
2185 
2186  // define a function-macro
2187  if (line[endOfTag] == '(') {
2188  defs[tag] = "";
2189  }
2190  // define value
2191  else {
2192  ++endOfTag;
2193 
2194  const std::string& value = line.substr(endOfTag, line.size()-endOfTag);
2195 
2196  if (defs.find(value) != defs.end())
2197  defs[tag] = defs[value];
2198  else
2199  defs[tag] = value;
2200  }
2201  }
2202 
2203  if (undefs.find(tag) != undefs.end()) {
2204  defs.erase(tag);
2205  }
2206  }
2207 
2208  else if (!suppressCurrentCodePath && line.compare(0,7,"#undef ") == 0) {
2209  defs.erase(line.substr(7));
2210  }
2211 
2212  else if (!suppressCurrentCodePath && line.compare(0,9,"#include ")==0) {
2213  std::string filename(line.substr(9));
2214 
2215  const HeaderTypes headerType = getHeaderFileName(filename);
2216  if (headerType == NoHeader) {
2217  ostr << std::endl;
2218  continue;
2219  }
2220 
2221  // try to open file
2222  std::string filepath;
2223  if (headerType == UserHeader)
2224  filepath = path;
2225  std::ifstream fin;
2226  if (!openHeader(filename, includePaths, filepath, fin)) {
2228  linenr,
2229  filename,
2230  headerType
2231  );
2232  ostr << std::endl;
2233  continue;
2234  }
2235 
2236  // Prevent that files are recursively included
2237  if (std::find(includes.begin(), includes.end(), filename) != includes.end()) {
2238  ostr << std::endl;
2239  continue;
2240  }
2241 
2242  includes.push_back(filename);
2243 
2244  // Don't include header if it's already included and contains #pragma once
2245  if (pragmaOnce.find(filename) != pragmaOnce.end()) {
2246  ostr << std::endl;
2247  continue;
2248  }
2249 
2250  ostr << "#file \"" << filename << "\"\n"
2251  << handleIncludes(read(fin, filename), filename, includePaths, defs, pragmaOnce, includes) << std::endl
2252  << "#endfile\n";
2253  continue;
2254  }
2255 
2256  if (!suppressCurrentCodePath)
2257  ostr << line;
2258  }
2259 
2260  // A line has been read..
2261  ostr << "\n";
2262  }
2263 
2264  return ostr.str();
2265 }
2266 
2267 
2268 void Preprocessor::handleIncludes(std::string &code, const std::string &filePath, const std::list<std::string> &includePaths)
2269 {
2270  std::list<std::string> paths;
2271  std::string path;
2272  path = filePath;
2273  path.erase(1 + path.find_last_of("\\/"));
2274  paths.push_back(path);
2275  std::string::size_type pos = 0;
2276  std::string::size_type endfilePos = 0;
2277  std::set<std::string> handledFiles;
2278  while ((pos = code.find("#include", pos)) != std::string::npos) {
2279  if (_settings && _settings->terminated())
2280  return;
2281 
2282  // Accept only includes that are at the start of a line
2283  if (pos > 0 && code[pos-1] != '\n') {
2284  pos += 8; // length of "#include"
2285  continue;
2286  }
2287 
2288  // If endfile is encountered, we have moved to a next file in our stack,
2289  // so remove last path in our list.
2290  while (!paths.empty() && (endfilePos = code.find("\n#endfile", endfilePos)) != std::string::npos && endfilePos < pos) {
2291  paths.pop_back();
2292  endfilePos += 9; // size of #endfile
2293  }
2294 
2295  endfilePos = pos;
2296  std::string::size_type end = code.find("\n", pos);
2297  std::string filename = code.substr(pos, end - pos);
2298 
2299  // Remove #include clause
2300  code.erase(pos, end - pos);
2301 
2302  HeaderTypes headerType = getHeaderFileName(filename);
2303  if (headerType == NoHeader)
2304  continue;
2305 
2306  // filename contains now a file name e.g. "menu.h"
2307  std::string processedFile;
2308  std::string filepath;
2309  if (headerType == UserHeader && !paths.empty())
2310  filepath = paths.back();
2311  std::ifstream fin;
2312  const bool fileOpened(openHeader(filename, includePaths, filepath, fin));
2313 
2314  if (fileOpened) {
2315  filename = Path::simplifyPath(filename);
2316  std::string tempFile = filename;
2317  std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper);
2318  if (handledFiles.find(tempFile) != handledFiles.end()) {
2319  // We have processed this file already once, skip
2320  // it this time to avoid eternal loop.
2321  fin.close();
2322  continue;
2323  }
2324 
2325  handledFiles.insert(tempFile);
2326  processedFile = Preprocessor::read(fin, filename);
2327  fin.close();
2328  }
2329 
2330  if (!processedFile.empty()) {
2331  // Remove space characters that are after or before new line character
2332  processedFile = "#file \"" + Path::fromNativeSeparators(filename) + "\"\n" + processedFile + "\n#endfile";
2333  code.insert(pos, processedFile);
2334 
2335  path = filename;
2336  path.erase(1 + path.find_last_of("\\/"));
2337  paths.push_back(path);
2338  } else if (!fileOpened && _settings) {
2339  std::string f = filePath;
2340 
2341  // Determine line number of include
2342  unsigned int linenr = 1;
2343  unsigned int level = 0;
2344  for (std::string::size_type p = 1; p <= pos; ++p) {
2345  if (level == 0 && code[pos-p] == '\n')
2346  ++linenr;
2347  else if (code.compare(pos-p, 9, "#endfile\n") == 0) {
2348  ++level;
2349  } else if (code.compare(pos-p, 6, "#file ") == 0) {
2350  if (level == 0) {
2351  linenr--;
2352  const std::string::size_type pos1 = pos - p + 7;
2353  const std::string::size_type pos2 = code.find_first_of("\"\n", pos1);
2354  f = code.substr(pos1, (pos2 == std::string::npos) ? pos2 : (pos2 - pos1));
2355  break;
2356  }
2357  --level;
2358  }
2359  }
2360 
2362  linenr,
2363  filename,
2364  headerType);
2365  }
2366  }
2367 }
2368 
2369 // Report that include is missing
2370 void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType)
2371 {
2372  const std::string fname = Path::fromNativeSeparators(filename);
2373  if (_settings->nomsg.isSuppressed("missingInclude", fname, linenr))
2374  return;
2375  if (headerType == SystemHeader && _settings->nomsg.isSuppressed("missingIncludeSystem", fname, linenr))
2376  return;
2377 
2378  if (headerType == SystemHeader)
2379  missingSystemIncludeFlag = true;
2380  else
2381  missingIncludeFlag = true;
2383 
2384  std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
2385  if (!filename.empty()) {
2387  loc.line = linenr;
2388  loc.setfile(Path::toNativeSeparators(filename));
2389  locationList.push_back(loc);
2390  }
2391  ErrorLogger::ErrorMessage errmsg(locationList, Severity::information,
2392  (headerType==SystemHeader) ?
2393  "Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results." :
2394  "Include file: \"" + header + "\" not found.",
2395  (headerType==SystemHeader) ? "missingIncludeSystem" : "missingInclude",
2396  false);
2397  errmsg.file0 = file0;
2398  _errorLogger->reportInfo(errmsg);
2399  }
2400 }
2401 
2402 /**
2403  * Skip string in line. A string begins and ends with either a &quot; or a &apos;
2404  * @param line the string
2405  * @param pos in=start position of string, out=end position of string
2406  */
2407 static void skipstring(const std::string &line, std::string::size_type &pos)
2408 {
2409  const char ch = line[pos];
2410 
2411  ++pos;
2412  while (pos < line.size() && line[pos] != ch) {
2413  if (line[pos] == '\\')
2414  ++pos;
2415  ++pos;
2416  }
2417 }
2418 
2419 /**
2420  * Remove heading and trailing whitespaces from the input parameter.
2421  * @param s The string to trim.
2422  */
2423 static std::string trim(const std::string& s)
2424 {
2425  std::string::size_type beg = s.find_first_not_of(" \t");
2426  if (beg == std::string::npos)
2427  return s;
2428  std::string::size_type end = s.find_last_not_of(" \t");
2429  if (end == std::string::npos)
2430  return s.substr(beg);
2431  return s.substr(beg, end - beg + 1);
2432 }
2433 
2434 /**
2435  * @brief get parameters from code. For example 'foo(1,2)' => '1','2'
2436  * @param line in: The code
2437  * @param pos in: Position to the '('. out: Position to the ')'
2438  * @param params out: The extracted parameters
2439  * @param numberOfNewlines out: number of newlines in the macro call
2440  * @param endFound out: was the end parentheses found?
2441  */
2442 static void getparams(const std::string &line,
2443  std::string::size_type &pos,
2444  std::vector<std::string> &params,
2445  unsigned int &numberOfNewlines,
2446  bool &endFound)
2447 {
2448  params.clear();
2449  numberOfNewlines = 0;
2450  endFound = false;
2451 
2452  if (line[pos] == ' ')
2453  pos++;
2454 
2455  if (line[pos] != '(')
2456  return;
2457 
2458  // parentheses level
2459  int parlevel = 0;
2460 
2461  // current parameter data
2462  std::string par;
2463 
2464  // scan for parameters..
2465  for (; pos < line.length(); ++pos) {
2466  // increase parentheses level
2467  if (line[pos] == '(') {
2468  ++parlevel;
2469  if (parlevel == 1)
2470  continue;
2471  }
2472 
2473  // decrease parentheses level
2474  else if (line[pos] == ')') {
2475  --parlevel;
2476  if (parlevel <= 0) {
2477  endFound = true;
2478  params.push_back(trim(par));
2479  break;
2480  }
2481  }
2482 
2483  // string
2484  else if (line[pos] == '\"' || line[pos] == '\'') {
2485  const std::string::size_type p = pos;
2486  skipstring(line, pos);
2487  if (pos == line.length())
2488  break;
2489  par += line.substr(p, pos + 1 - p);
2490  continue;
2491  }
2492 
2493  // count newlines. the expanded macro must have the same number of newlines
2494  else if (line[pos] == '\n') {
2495  ++numberOfNewlines;
2496  continue;
2497  }
2498 
2499  // new parameter
2500  if (parlevel == 1 && line[pos] == ',') {
2501  params.push_back(trim(par));
2502  par = "";
2503  }
2504 
2505  // spaces are only added if needed
2506  else if (line[pos] == ' ') {
2507  // Add space only if it is needed
2508  if (par.size() && std::isalnum((unsigned char)par[par.length()-1])) {
2509  par += ' ';
2510  }
2511  }
2512 
2513  // add character to current parameter
2514  else if (parlevel >= 1 && line[pos] != Preprocessor::macroChar) {
2515  par.append(1, line[pos]);
2516  }
2517  }
2518 }
2519 
2520 /** @brief Class that the preprocessor uses when it expands macros. This class represents a preprocessor macro */
2522 private:
2524 
2525  /** tokens of this macro */
2527 
2528  /** macro parameters */
2529  std::vector<std::string> _params;
2530 
2531  /** name of macro */
2532  std::string _name;
2533 
2534  /** macro definition in plain text */
2535  const std::string _macro;
2536 
2537  /** prefix that is used by cppcheck to separate macro parameters. Always "__cppcheck__" */
2538  const std::string _prefix;
2539 
2540  /** does this macro take a variable number of parameters? */
2542 
2543  /** The macro has parentheses but no parameters.. "AAA()" */
2544  bool _nopar;
2545 
2546  /** disabled assignment operator */
2547  void operator=(const PreprocessorMacro &);
2548 
2549  /** @brief expand inner macro */
2550  std::vector<std::string> expandInnerMacros(const std::vector<std::string> &params1,
2551  const std::map<std::string, PreprocessorMacro *> &macros) const {
2552  std::string innerMacroName;
2553 
2554  // Is there an inner macro..
2555  {
2556  const Token *tok = Token::findsimplematch(tokens(), ")");
2557  if (!Token::Match(tok, ") %var% ("))
2558  return params1;
2559  innerMacroName = tok->strAt(1);
2560  tok = tok->tokAt(3);
2561  unsigned int par = 0;
2562  while (Token::Match(tok, "%var% ,|)")) {
2563  tok = tok->tokAt(2);
2564  par++;
2565  }
2566  if (tok || par != params1.size())
2567  return params1;
2568  }
2569 
2570  std::vector<std::string> params2(params1);
2571 
2572  for (std::size_t ipar = 0; ipar < params1.size(); ++ipar) {
2573  const std::string s(innerMacroName + "(");
2574  std::string param(params1[ipar]);
2575  if (param.compare(0,s.length(),s)==0 && param[param.length()-1]==')') {
2576  std::vector<std::string> innerparams;
2577  std::string::size_type pos = s.length() - 1;
2578  unsigned int num = 0;
2579  bool endFound = false;
2580  getparams(param, pos, innerparams, num, endFound);
2581  if (pos == param.length()-1 && num==0 && endFound && innerparams.size() == params1.size()) {
2582  // Is inner macro defined?
2583  std::map<std::string, PreprocessorMacro *>::const_iterator it = macros.find(innerMacroName);
2584  if (it != macros.end()) {
2585  // expand the inner macro
2586  const PreprocessorMacro *innerMacro = it->second;
2587 
2588  std::string innercode;
2589  std::map<std::string,PreprocessorMacro *> innermacros = macros;
2590  innermacros.erase(innerMacroName);
2591  innerMacro->code(innerparams, innermacros, innercode);
2592  params2[ipar] = innercode;
2593  }
2594  }
2595  }
2596  }
2597 
2598  return params2;
2599  }
2600 
2601 public:
2602  /**
2603  * @brief Constructor for PreprocessorMacro. This is the "setter"
2604  * for this class - everything is setup here.
2605  * @param macro The code after define, until end of line,
2606  * e.g. "A(x) foo(x);"
2607  */
2608  explicit PreprocessorMacro(const std::string &macro)
2609  : _macro(macro), _prefix("__cppcheck__") {
2611 
2612  // Tokenize the macro to make it easier to handle
2613  std::istringstream istr(macro);
2614  tokenizer.list.createTokens(istr);
2615 
2616  // macro name..
2617  if (tokens() && tokens()->isName())
2618  _name = tokens()->str();
2619 
2620  // initialize parameters to default values
2621  _variadic = _nopar = false;
2622 
2623  std::string::size_type pos = macro.find_first_of(" (");
2624  if (pos != std::string::npos && macro[pos] == '(') {
2625  // Extract macro parameters
2626  if (Token::Match(tokens(), "%var% ( %var%")) {
2627  for (const Token *tok = tokens()->tokAt(2); tok; tok = tok->next()) {
2628  if (tok->str() == ")")
2629  break;
2630  if (Token::simpleMatch(tok, ". . . )")) {
2631  if (tok->previous()->str() == ",")
2632  _params.push_back("__VA_ARGS__");
2633  _variadic = true;
2634  break;
2635  }
2636  if (tok->isName())
2637  _params.push_back(tok->str());
2638  }
2639  }
2640 
2641  else if (Token::Match(tokens(), "%var% ( . . . )"))
2642  _variadic = true;
2643 
2644  else if (Token::Match(tokens(), "%var% ( )"))
2645  _nopar = true;
2646  }
2647  }
2648 
2649  /** return tokens of this macro */
2650  const Token *tokens() const {
2651  return tokenizer.tokens();
2652  }
2653 
2654  /** read parameters of this macro */
2655  const std::vector<std::string> &params() const {
2656  return _params;
2657  }
2658 
2659  /** check if this is macro has a variable number of parameters */
2660  bool variadic() const {
2661  return _variadic;
2662  }
2663 
2664  /** Check if this macro has parentheses but no parameters */
2665  bool nopar() const {
2666  return _nopar;
2667  }
2668 
2669  /** name of macro */
2670  const std::string &name() const {
2671  return _name;
2672  }
2673 
2674  /**
2675  * get expanded code for this macro
2676  * @param params2 macro parameters
2677  * @param macros macro definitions (recursion)
2678  * @param macrocode output string
2679  * @return true if the expanding was successful
2680  */
2681  bool code(const std::vector<std::string> &params2, const std::map<std::string, PreprocessorMacro *> &macros, std::string &macrocode) const {
2682  if (_nopar || (_params.empty() && _variadic)) {
2683  macrocode = _macro.substr(1 + _macro.find(")"));
2684  if (macrocode.empty())
2685  return true;
2686 
2687  std::string::size_type pos = 0;
2688  // Remove leading spaces
2689  if ((pos = macrocode.find_first_not_of(" ")) > 0)
2690  macrocode.erase(0, pos);
2691  // Remove ending newline
2692  if ((pos = macrocode.find_first_of("\r\n")) != std::string::npos)
2693  macrocode.erase(pos);
2694 
2695  // Replace "__VA_ARGS__" with parameters
2696  if (!_nopar) {
2697  std::string s;
2698  for (std::size_t i = 0; i < params2.size(); ++i) {
2699  if (i > 0)
2700  s += ",";
2701  s += params2[i];
2702  }
2703 
2704  pos = 0;
2705  while ((pos = macrocode.find("__VA_ARGS__", pos)) != std::string::npos) {
2706  macrocode.erase(pos, 11);
2707  macrocode.insert(pos, s);
2708  pos += s.length();
2709  }
2710  }
2711  }
2712 
2713  else if (_params.empty()) {
2714  std::string::size_type pos = _macro.find_first_of(" \"");
2715  if (pos == std::string::npos)
2716  macrocode = "";
2717  else {
2718  if (_macro[pos] == ' ')
2719  pos++;
2720  macrocode = _macro.substr(pos);
2721  if ((pos = macrocode.find_first_of("\r\n")) != std::string::npos)
2722  macrocode.erase(pos);
2723  }
2724  }
2725 
2726  else {
2727  const std::vector<std::string> givenparams = expandInnerMacros(params2, macros);
2728 
2729  const Token *tok = tokens();
2730  while (tok && tok->str() != ")")
2731  tok = tok->next();
2732  if (tok) {
2733  bool optcomma = false;
2734  while (nullptr != (tok = tok->next())) {
2735  std::string str = tok->str();
2736  if (str == "##")
2737  continue;
2738  if (str[0] == '#' || tok->isName()) {
2739  const bool stringify(str[0] == '#');
2740  if (stringify) {
2741  str = str.erase(0, 1);
2742  }
2743  for (std::size_t i = 0; i < _params.size(); ++i) {
2744  if (str == _params[i]) {
2745  if (_variadic &&
2746  (i == _params.size() - 1 ||
2747  (givenparams.size() + 2 == _params.size() && i + 1 == _params.size() - 1))) {
2748  str = "";
2749  for (std::size_t j = _params.size() - 1; j < givenparams.size(); ++j) {
2750  if (optcomma || j > _params.size() - 1)
2751  str += ",";
2752  optcomma = false;
2753  str += givenparams[j];
2754  }
2755  } else if (i >= givenparams.size()) {
2756  // Macro had more parameters than caller used.
2757  macrocode = "";
2758  return false;
2759  } else if (stringify) {
2760  const std::string &s(givenparams[i]);
2761  std::ostringstream ostr;
2762  ostr << "\"";
2763  for (std::string::size_type j = 0; j < s.size(); ++j) {
2764  if (s[j] == '\\' || s[j] == '\"')
2765  ostr << '\\';
2766  ostr << s[j];
2767  }
2768  str = ostr.str() + "\"";
2769  } else
2770  str = givenparams[i];
2771 
2772  break;
2773  }
2774  }
2775 
2776  // expand nopar macro
2777  if (tok->strAt(-1) != "##") {
2778  const std::map<std::string, PreprocessorMacro *>::const_iterator it = macros.find(str);
2779  if (it != macros.end() && it->second->_macro.find("(") == std::string::npos) {
2780  str = it->second->_macro;
2781  if (str.find(" ") != std::string::npos)
2782  str.erase(0, str.find(" "));
2783  else
2784  str = "";
2785  }
2786  }
2787  }
2788  if (_variadic && tok->str() == "," && tok->next() && tok->next()->str() == "##") {
2789  optcomma = true;
2790  continue;
2791  }
2792  optcomma = false;
2793  macrocode += str;
2794  if (Token::Match(tok, "%var% %var%") ||
2795  Token::Match(tok, "%var% %num%") ||
2796  Token::Match(tok, "%num% %var%") ||
2797  Token::simpleMatch(tok, "> >"))
2798  macrocode += " ";
2799  }
2800  }
2801  }
2802 
2803  return true;
2804  }
2805 };
2806 
2807 /**
2808  * Get data from a input string. This is an extended version of std::getline.
2809  * The std::getline only get a single line at a time. It can therefore happen that it
2810  * contains a partial statement. This function ensures that the returned data
2811  * doesn't end in the middle of a statement. The "getlines" name indicate that
2812  * this function will return multiple lines if needed.
2813  * @param istr input stream
2814  * @param line output data
2815  * @return success
2816  */
2817 static bool getlines(std::istream &istr, std::string &line)
2818 {
2819  if (!istr.good())
2820  return false;
2821  line = "";
2822  int parlevel = 0;
2823  for (char ch = (char)istr.get(); istr.good(); ch = (char)istr.get()) {
2824  if (ch == '\'' || ch == '\"') {
2825  line += ch;
2826  char c = 0;
2827  while (istr.good() && c != ch) {
2828  if (c == '\\') {
2829  c = (char)istr.get();
2830  if (!istr.good())
2831  return true;
2832  line += c;
2833  }
2834 
2835  c = (char)istr.get();
2836  if (!istr.good())
2837  return true;
2838  if (c == '\n' && line.compare(0, 1, "#") == 0)
2839  return true;
2840  line += c;
2841  }
2842  continue;
2843  }
2844  if (ch == '(')
2845  ++parlevel;
2846  else if (ch == ')')
2847  --parlevel;
2848  else if (ch == '\n') {
2849  if (line.compare(0, 1, "#") == 0)
2850  return true;
2851 
2852  if (istr.peek() == '#') {
2853  line += ch;
2854  return true;
2855  }
2856  } else if (line.compare(0, 1, "#") != 0 && parlevel <= 0 && ch == ';') {
2857  line += ";";
2858  return true;
2859  }
2860 
2861  line += ch;
2862  }
2863  return true;
2864 }
2865 
2866 bool Preprocessor::validateCfg(const std::string &code, const std::string &cfg)
2867 {
2868  // fill up "macros" with empty configuration macros
2869  std::set<std::string> macros;
2870  for (std::string::size_type pos = 0; pos < cfg.size();) {
2871  const std::string::size_type pos2 = cfg.find_first_of(";=", pos);
2872  if (pos2 == std::string::npos) {
2873  macros.insert(cfg.substr(pos));
2874  break;
2875  }
2876  if (cfg[pos2] == ';')
2877  macros.insert(cfg.substr(pos, pos2-pos));
2878  pos = cfg.find(";", pos2);
2879  if (pos != std::string::npos)
2880  ++pos;
2881  }
2882 
2883  // check if any empty macros are used in code
2884  for (std::set<std::string>::const_iterator it = macros.begin(); it != macros.end(); ++it) {
2885  const std::string &macro = *it;
2886  std::string::size_type pos = 0;
2887  while ((pos = code.find_first_of(std::string("#\"'")+macro[0], pos)) != std::string::npos) {
2888  const std::string::size_type pos1 = pos;
2889  const std::string::size_type pos2 = pos + macro.size();
2890  pos++;
2891 
2892  // skip string..
2893  if (code[pos1] == '\"' || code[pos1] == '\'') {
2894  while (pos < code.size() && code[pos] != code[pos1]) {
2895  if (code[pos] == '\\')
2896  ++pos;
2897  ++pos;
2898  }
2899  ++pos;
2900  }
2901 
2902  // skip preprocessor statement..
2903  else if (code[pos1] == '#') {
2904  if (pos1 == 0 || code[pos1-1] == '\n')
2905  pos = code.find("\n",pos);
2906  }
2907 
2908  // is macro used in code?
2909  else if (code.compare(pos1,macro.size(),macro) == 0) {
2910  if (pos1 > 0 && (std::isalnum((unsigned char)code[pos1-1U]) || code[pos1-1U] == '_'))
2911  continue;
2912  if (pos2 < code.size() && (std::isalnum((unsigned char)code[pos2]) || code[pos2] == '_'))
2913  continue;
2914  // macro is used in code, return false
2915  if (_settings->isEnabled("information"))
2916  validateCfgError(cfg, macro);
2917  return false;
2918  }
2919  }
2920  }
2921 
2922  return true;
2923 }
2924 
2925 void Preprocessor::validateCfgError(const std::string &cfg, const std::string &macro)
2926 {
2927  const std::string id = "ConfigurationNotChecked";
2928  std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
2930  loc.line = 1;
2931  loc.setfile(file0);
2932  locationList.push_back(loc);
2933  ErrorLogger::ErrorMessage errmsg(locationList, Severity::information, "Skipping configuration '" + cfg + "' since the value of '" + macro + "' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.", id, false);
2934  _errorLogger->reportInfo(errmsg);
2935 }
2936 
2937 std::string Preprocessor::expandMacros(const std::string &code, std::string filename, const std::string &cfg, ErrorLogger *errorLogger)
2938 {
2939  // Search for macros and expand them..
2940  // --------------------------------------------
2941 
2942  // Available macros (key=macroname, value=macro).
2943  std::map<std::string, PreprocessorMacro *> macros;
2944 
2945  {
2946  // fill up "macros" with user defined macros
2947  const std::map<std::string,std::string> cfgmap(getcfgmap(cfg,nullptr,""));
2948  std::map<std::string, std::string>::const_iterator it;
2949  for (it = cfgmap.begin(); it != cfgmap.end(); ++it) {
2950  std::string s = it->first;
2951  if (!it->second.empty())
2952  s += " " + it->second;
2953  PreprocessorMacro *macro = new PreprocessorMacro(s);
2954  macros[it->first] = macro;
2955  }
2956  }
2957 
2958  // Current line number
2959  unsigned int linenr = 1;
2960 
2961  // linenr, filename
2962  std::stack< std::pair<unsigned int, std::string> > fileinfo;
2963 
2964  // output stream
2965  std::ostringstream ostr;
2966 
2967  // read code..
2968  std::istringstream istr(code);
2969  std::string line;
2970  while (getlines(istr, line)) {
2971  // defining a macro..
2972  if (line.compare(0, 8, "#define ") == 0) {
2973  PreprocessorMacro *macro = new PreprocessorMacro(line.substr(8));
2974  if (macro->name().empty() || macro->name() == "NULL") {
2975  delete macro;
2976  } else if (macro->name() == "BOOST_FOREACH") {
2977  // BOOST_FOREACH is currently too complex to parse, so skip it.
2978  delete macro;
2979  } else {
2980  std::map<std::string, PreprocessorMacro *>::iterator it;
2981  it = macros.find(macro->name());
2982  if (it != macros.end())
2983  delete it->second;
2984  macros[macro->name()] = macro;
2985  }
2986  line = "\n";
2987  }
2988 
2989  // undefining a macro..
2990  else if (line.compare(0, 7, "#undef ") == 0) {
2991  std::map<std::string, PreprocessorMacro *>::iterator it;
2992  it = macros.find(line.substr(7));
2993  if (it != macros.end()) {
2994  delete it->second;
2995  macros.erase(it);
2996  }
2997  line = "\n";
2998  }
2999 
3000  // entering a file, update position..
3001  else if (line.compare(0, 7, "#file \"") == 0) {
3002  fileinfo.push(std::pair<unsigned int, std::string>(linenr, filename));
3003  filename = line.substr(7, line.length() - 8);
3004  linenr = 0;
3005  line += "\n";
3006  }
3007 
3008  // leaving a file, update position..
3009  else if (line == "#endfile") {
3010  if (!fileinfo.empty()) {
3011  linenr = fileinfo.top().first;
3012  filename = fileinfo.top().second;
3013  fileinfo.pop();
3014  }
3015  line += "\n";
3016  }
3017 
3018  // all other preprocessor directives are just replaced with a newline
3019  else if (line.compare(0, 1, "#") == 0) {
3020  line += "\n";
3021  }
3022 
3023  // expand macros..
3024  else {
3025  // Limit for each macro.
3026  // The limit specify a position in the "line" variable.
3027  // For a "recursive macro" where the expanded text contains
3028  // the macro again, the macro should not be expanded again.
3029  // The limits are used to prevent recursive expanding.
3030  // * When a macro is expanded its limit position is set to
3031  // the last expanded character.
3032  // * macros are only allowed to be expanded when the
3033  // the position is beyond the limit.
3034  // * The limit is relative to the end of the "line"
3035  // variable. Inserting and deleting text before the limit
3036  // without updating the limit is safe.
3037  // * when pos goes beyond a limit the limit needs to be
3038  // deleted because it is unsafe to insert/delete text
3039  // after the limit otherwise
3040  std::map<const PreprocessorMacro *, std::size_t> limits;
3041 
3042  // pos is the current position in line
3043  std::string::size_type pos = 0;
3044 
3045  // scan line to see if there are any macros to expand..
3046  unsigned int tmpLinenr = 0;
3047  while (pos < line.size()) {
3048  if (line[pos] == '\n')
3049  ++tmpLinenr;
3050 
3051  // skip strings..
3052  if (line[pos] == '\"' || line[pos] == '\'') {
3053  const char ch = line[pos];
3054 
3055  skipstring(line, pos);
3056  ++pos;
3057 
3058  if (pos >= line.size()) {
3059  writeError(filename,
3060  linenr + tmpLinenr,
3061  errorLogger,
3062  "noQuoteCharPair",
3063  std::string("No pair for character (") + ch + "). Can't process file. File is either invalid or unicode, which is currently not supported.");
3064 
3065  std::map<std::string, PreprocessorMacro *>::iterator it;
3066  for (it = macros.begin(); it != macros.end(); ++it)
3067  delete it->second;
3068  macros.clear();
3069  return "";
3070  }
3071 
3072  continue;
3073  }
3074 
3075  if (!std::isalpha((unsigned char)line[pos]) && line[pos] != '_')
3076  ++pos;
3077 
3078  // found an identifier..
3079  // the "while" is used in case the expanded macro will immediately call another macro
3080  while (pos < line.length() && (std::isalpha((unsigned char)line[pos]) || line[pos] == '_')) {
3081  // pos1 = start position of macro
3082  const std::string::size_type pos1 = pos++;
3083 
3084  // find the end of the identifier
3085  while (pos < line.size() && (std::isalnum((unsigned char)line[pos]) || line[pos] == '_'))
3086  ++pos;
3087 
3088  // get identifier
3089  const std::string id = line.substr(pos1, pos - pos1);
3090 
3091  // is there a macro with this name?
3092  std::map<std::string, PreprocessorMacro *>::const_iterator it;
3093  it = macros.find(id);
3094  if (it == macros.end())
3095  break; // no macro with this name exist
3096 
3097  const PreprocessorMacro * const macro = it->second;
3098 
3099  // check that pos is within allowed limits for this
3100  // macro
3101  {
3102  const std::map<const PreprocessorMacro *, std::size_t>::const_iterator it2 = limits.find(macro);
3103  if (it2 != limits.end() && pos <= line.length() - it2->second)
3104  break;
3105  }
3106 
3107  // get parameters from line..
3108  std::vector<std::string> params;
3109  std::string::size_type pos2 = pos;
3110  if (macro->params().size() && pos2 >= line.length())
3111  break;
3112 
3113  // number of newlines within macro use
3114  unsigned int numberOfNewlines = 0;
3115 
3116  // if the macro has parentheses, get parameters
3117  if (macro->variadic() || macro->nopar() || macro->params().size()) {
3118  // is the end parentheses found?
3119  bool endFound = false;
3120 
3121  getparams(line,pos2,params,numberOfNewlines,endFound);
3122 
3123  // something went wrong so bail out
3124  if (!endFound)
3125  break;
3126  }
3127 
3128  // Just an empty parameter => clear
3129  if (params.size() == 1 && params[0] == "")
3130  params.clear();
3131 
3132  // Check that it's the same number of parameters..
3133  if (!macro->variadic() && params.size() != macro->params().size())
3134  break;
3135 
3136  // Create macro code..
3137  std::string tempMacro;
3138  if (!macro->code(params, macros, tempMacro)) {
3139  // Syntax error in code
3140  writeError(filename,
3141  linenr + tmpLinenr,
3142  errorLogger,
3143  "syntaxError",
3144  std::string("Syntax error. Not enough parameters for macro '") + macro->name() + "'.");
3145 
3146  std::map<std::string, PreprocessorMacro *>::iterator iter;
3147  for (iter = macros.begin(); iter != macros.end(); ++iter)
3148  delete iter->second;
3149  macros.clear();
3150  return "";
3151  }
3152 
3153  // make sure number of newlines remain the same..
3154  std::string macrocode(std::string(numberOfNewlines, '\n') + tempMacro);
3155 
3156  // Insert macro code..
3157  if (macro->variadic() || macro->nopar() || !macro->params().empty())
3158  ++pos2;
3159 
3160  // Remove old limits
3161  for (std::map<const PreprocessorMacro *, std::size_t>::iterator iter = limits.begin();
3162  iter != limits.end();) {
3163  if ((line.length() - pos1) < iter->second) {
3164  // We have gone past this limit, so just delete it
3165  limits.erase(iter++);
3166  } else {
3167  ++iter;
3168  }
3169  }
3170 
3171  // don't allow this macro to be expanded again before pos2
3172  limits[macro] = line.length() - pos2;
3173 
3174  // erase macro
3175  line.erase(pos1, pos2 - pos1);
3176 
3177  // Don't glue this macro into variable or number after it
3178  if (!line.empty() && (std::isalnum((unsigned char)line[pos1]) || line[pos1] == '_'))
3179  macrocode.append(1,' ');
3180 
3181  // insert macrochar before each symbol/nr/operator
3182  bool str = false;
3183  bool chr = false;
3184  for (std::size_t i = 0U; i < macrocode.size(); ++i) {
3185  if (macrocode[i] == '\\') {
3186  i++;
3187  continue;
3188  } else if (macrocode[i] == '\"')
3189  str = !str;
3190  else if (macrocode[i] == '\'')
3191  chr = !chr;
3192  else if (str || chr)
3193  continue;
3194  else if (macrocode[i] == '.') { // 5. / .5
3195  if ((i > 0U && std::isdigit((unsigned char)macrocode[i-1])) ||
3196  (i+1 < macrocode.size() && std::isdigit((unsigned char)macrocode[i+1]))) {
3197  if (i > 0U && !std::isdigit((unsigned char)macrocode[i-1])) {
3198  macrocode.insert(i, 1U, macroChar);
3199  i++;
3200  }
3201  i++;
3202  if (i<macrocode.size() && std::isdigit((unsigned char)macrocode[i]))
3203  i++;
3204  if (i+1U < macrocode.size() &&
3205  (macrocode[i] == 'e' || macrocode[i] == 'E') &&
3206  (macrocode[i+1] == '+' || macrocode[i+1] == '-')) {
3207  i+=2;
3208  }
3209  }
3210  } else if (std::isalnum((unsigned char)macrocode[i]) || macrocode[i] == '_') {
3211  if ((i > 0U) &&
3212  (!std::isalnum((unsigned char)macrocode[i-1])) &&
3213  (macrocode[i-1] != '_') &&
3214  (macrocode[i-1] != macroChar)) {
3215  macrocode.insert(i, 1U, macroChar);
3216  }
3217 
3218  // 1e-7 / 1e+7
3219  if (i+3U < macrocode.size() &&
3220  (std::isdigit((unsigned char)macrocode[i]) || macrocode[i]=='.') &&
3221  (macrocode[i+1] == 'e' || macrocode[i+1] == 'E') &&
3222  (macrocode[i+2] == '-' || macrocode[i+2] == '+') &&
3223  std::isdigit((unsigned char)macrocode[i+3])) {
3224  i += 3U;
3225  }
3226 
3227  // 1.f / 1.e7
3228  if (i+2U < macrocode.size() &&
3229  std::isdigit((unsigned char)macrocode[i]) &&
3230  macrocode[i+1] == '.' &&
3231  std::isalpha((unsigned char)macrocode[i+2])) {
3232  i += 2U;
3233  if (i+2U < macrocode.size() &&
3234  (macrocode[i+0] == 'e' || macrocode[i+0] == 'E') &&
3235  (macrocode[i+1] == '-' || macrocode[i+1] == '+') &&
3236  std::isdigit((unsigned char)macrocode[i+2])) {
3237  i += 2U;
3238  }
3239  }
3240  }
3241  }
3242  line.insert(pos1, macroChar + macrocode);
3243 
3244  // position = start position.
3245  pos = pos1;
3246  }
3247  }
3248  }
3249 
3250  // the line has been processed in various ways. Now add it to the output stream
3251  ostr << line;
3252 
3253  // update linenr
3254  for (std::string::size_type p = 0; p < line.length(); ++p) {
3255  if (line[p] == '\n')
3256  ++linenr;
3257  }
3258  }
3259 
3260  for (std::map<std::string, PreprocessorMacro *>::iterator it = macros.begin(); it != macros.end(); ++it)
3261  delete it->second;
3262  macros.clear();
3263 
3264  return ostr.str();
3265 }
3266 
3267 
3268 void Preprocessor::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
3269 {
3270  Settings settings2(*settings);
3271  Preprocessor preprocessor(&settings2, errorLogger);
3272  settings2.checkConfiguration=true;
3273  preprocessor.missingInclude("", 1, "", UserHeader);
3274  preprocessor.missingInclude("", 1, "", SystemHeader);
3275  preprocessor.validateCfgError("X", "X");
3276  preprocessor.error("", 1, "#error message"); // #error ..
3277 }