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