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