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