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-2016 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 "path.h"
22 #include "errorlogger.h"
23 #include "settings.h"
24 #include "simplecpp.h"
25 
26 #include <algorithm>
27 #include <sstream>
28 #include <fstream>
29 #include <cstdlib>
30 #include <cctype>
31 #include <vector>
32 #include <set>
33 #include <stack>
34 
35 /**
36  * Remove heading and trailing whitespaces from the input parameter.
37  * If string is all spaces/tabs, return empty string.
38  * @param s The string to trim.
39  */
40 static std::string trim(const std::string& s)
41 {
42  const std::string::size_type beg = s.find_first_not_of(" \t");
43  if (beg == std::string::npos)
44  return "";
45  const std::string::size_type end = s.find_last_not_of(" \t");
46  return s.substr(beg, end - beg + 1);
47 }
48 
49 Directive::Directive(const std::string &_file, const int _linenr, const std::string &_str):
50  file(_file),
51  linenr(_linenr),
52  str(_str)
53 {
54  str = trim(str);
55 }
56 
59 
60 char Preprocessor::macroChar = char(1);
61 
62 Preprocessor::Preprocessor(Settings& settings, ErrorLogger *errorLogger) : _settings(settings), _errorLogger(errorLogger)
63 {
64 }
65 
67 {
68  for (std::map<std::string, simplecpp::TokenList *>::iterator it = tokenlists.begin(); it != tokenlists.end(); ++it)
69  delete it->second;
70 }
71 
72 void Preprocessor::writeError(const std::string &fileName, const unsigned int linenr, ErrorLogger *errorLogger, const std::string &errorType, const std::string &errorText)
73 {
74  if (!errorLogger)
75  return;
76 
77  std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
78  ErrorLogger::ErrorMessage::FileLocation loc(fileName, linenr);
79  locationList.push_back(loc);
80  errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList,
82  errorText,
83  errorType,
84  false));
85 }
86 
87 
88 static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &_settings)
89 {
90  std::list<std::string> suppressionIDs;
91 
92  for (const simplecpp::Token *tok = tokens.cbegin(); tok; tok = tok->next) {
93  if (tok->comment) {
94  std::istringstream iss(tok->str.substr(2));
95  std::string word;
96  iss >> word;
97  if (word != "cppcheck-suppress")
98  continue;
99  iss >> word;
100  if (iss)
101  suppressionIDs.push_back(word);
102  continue;
103  }
104 
105  if (suppressionIDs.empty())
106  continue;
107 
108  // Relative filename
109  std::string relativeFilename(tok->location.file());
110  if (_settings.relativePaths) {
111  for (std::size_t j = 0U; j < _settings.basePaths.size(); ++j) {
112  const std::string bp = _settings.basePaths[j] + "/";
113  if (relativeFilename.compare(0,bp.size(),bp)==0) {
114  relativeFilename = relativeFilename.substr(bp.size());
115  }
116  }
117  }
118 
119  // Add the suppressions.
120  for (std::list<std::string>::const_iterator it = suppressionIDs.begin(); it != suppressionIDs.end(); ++it) {
121  _settings.nomsg.addSuppression(*it, relativeFilename, tok->location.line);
122  }
123  suppressionIDs.clear();
124  }
125 }
126 
127 void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens)
128 {
130  return;
132  for (std::map<std::string,simplecpp::TokenList*>::const_iterator it = tokenlists.begin(); it != tokenlists.end(); ++it) {
133  if (it->second)
134  ::inlineSuppressions(*it->second, _settings);
135  }
136 }
137 
138 static bool sameline(const simplecpp::Token *tok1, const simplecpp::Token *tok2)
139 {
140  return tok1 && tok2 && tok1->location.sameline(tok2->location);
141 }
142 
143 static std::string readcondition(const simplecpp::Token *iftok, const std::set<std::string> &defined)
144 {
145  const simplecpp::Token *cond = iftok->next;
146  if (!sameline(iftok,cond))
147  return "";
148 
149  const simplecpp::Token *next1 = cond->next;
150  const simplecpp::Token *next2 = next1 ? next1->next : nullptr;
151  const simplecpp::Token *next3 = next2 ? next2->next : nullptr;
152 
153  unsigned int len = 1;
154  if (sameline(iftok,next1))
155  len = 2;
156  if (sameline(iftok,next2))
157  len = 3;
158  if (sameline(iftok,next3))
159  len = 4;
160 
161  if (len == 1 && cond->str == "0")
162  return "0";
163 
164  if (len == 1 && cond->name) {
165  if (defined.find(cond->str) == defined.end())
166  return cond->str;
167  }
168 
169  if (len == 3 && cond->op == '(' && next1->name && next2->op == ')') {
170  if (defined.find(next1->str) == defined.end())
171  return next1->str;
172  }
173 
174  if (len == 3 && cond->name && next1->str == "==" && next2->number) {
175  if (defined.find(cond->str) == defined.end())
176  return cond->str + '=' + cond->next->next->str;
177  }
178 
179  std::set<std::string> configset;
180  for (; sameline(iftok,cond); cond = cond->next) {
181  if (cond->op == '!')
182  break;
183  if (cond->str != "defined")
184  continue;
185  const simplecpp::Token *dtok = cond->next;
186  if (!dtok)
187  break;
188  if (dtok->op == '(')
189  dtok = dtok->next;
190  if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str) == defined.end())
191  configset.insert(dtok->str);
192  }
193  std::string cfg;
194  for (std::set<std::string>::const_iterator it = configset.begin(); it != configset.end(); ++it) {
195  if (!cfg.empty())
196  cfg += ';';
197  cfg += *it;
198  }
199  return cfg;
200 }
201 
202 static std::string cfg(const std::vector<std::string> &configs)
203 {
204  std::set<std::string> configs2(configs.begin(), configs.end());
205  std::string ret;
206  for (std::set<std::string>::const_iterator it = configs2.begin(); it != configs2.end(); ++it) {
207  if (it->empty())
208  continue;
209  if (*it == "0")
210  return "";
211  if (!ret.empty())
212  ret += ';';
213  ret += *it;
214  }
215  return ret;
216 }
217 
218 static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string> &defined, std::set<std::string> &ret)
219 {
220  std::vector<std::string> configs_if;
221  std::vector<std::string> configs_ifndef;
222 
223  for (const simplecpp::Token *tok = tokens.cbegin(); tok; tok = tok->next) {
224  if (tok->op != '#' || sameline(tok->previous, tok))
225  continue;
226  const simplecpp::Token *cmdtok = tok->next;
227  if (!sameline(tok, cmdtok))
228  continue;
229  if (cmdtok->str == "ifdef" || cmdtok->str == "ifndef" || cmdtok->str == "if") {
230  std::string config;
231  if (cmdtok->str == "ifdef" || cmdtok->str == "ifndef") {
232  const simplecpp::Token *expr1 = cmdtok->next;
233  if (sameline(tok,expr1) && expr1->name && !sameline(tok,expr1->next))
234  config = expr1->str;
235  if (defined.find(config) != defined.end())
236  config.clear();
237  } else if (cmdtok->str == "if") {
238  config = readcondition(cmdtok, defined);
239  }
240  configs_if.push_back((cmdtok->str == "ifndef") ? std::string() : config);
241  configs_ifndef.push_back((cmdtok->str == "ifndef") ? config : std::string());
242  ret.insert(cfg(configs_if));
243  } else if (cmdtok->str == "elif") {
244  if (!configs_if.empty())
245  configs_if.pop_back();
246  configs_if.push_back(readcondition(cmdtok, defined));
247  ret.insert(cfg(configs_if));
248  } else if (cmdtok->str == "else") {
249  if (!configs_if.empty())
250  configs_if.pop_back();
251  configs_if.push_back(configs_ifndef.back());
252  ret.insert(cfg(configs_if));
253  } else if (cmdtok->str == "endif" && !sameline(tok, cmdtok->next)) {
254  if (!configs_if.empty())
255  configs_if.pop_back();
256  if (!configs_ifndef.empty())
257  configs_ifndef.pop_back();
258  } else if (cmdtok->str == "define" && sameline(tok, cmdtok->next) && cmdtok->next->name) {
259  defined.insert(cmdtok->next->str);
260  }
261  }
262 }
263 
264 
265 std::set<std::string> Preprocessor::getConfigs(const simplecpp::TokenList &tokens) const
266 {
267  std::set<std::string> ret;
268  ret.insert("");
269  if (!tokens.cbegin())
270  return ret;
271 
272  std::set<std::string> defined;
273  defined.insert("__cplusplus");
274 
275  ::getConfigs(tokens, defined, ret);
276 
277  for (std::map<std::string, simplecpp::TokenList*>::const_iterator it = tokenlists.begin(); it != tokenlists.end(); ++it)
278  ::getConfigs(*(it->second), defined, ret);
279 
280  return ret;
281 }
282 
283 std::string Preprocessor::preprocessCleanupDirectives(const std::string &processedFile)
284 {
285  std::ostringstream code;
286  std::istringstream sstr(processedFile);
287 
288  std::string line;
289  while (std::getline(sstr, line)) {
290  // Trim lines..
291  if (!line.empty() && line[0] == ' ')
292  line.erase(0, line.find_first_not_of(" "));
293  if (!line.empty() && line.back() == ' ')
294  line.erase(line.find_last_not_of(" ") + 1);
295 
296  // Preprocessor
297  if (!line.empty() && line[0] == '#') {
298  enum {
299  ESC_NONE,
300  ESC_SINGLE,
301  ESC_DOUBLE
302  } escapeStatus = ESC_NONE;
303 
304  char prev = ' '; // hack to make it skip spaces between # and the directive
305  code << "#";
306  std::string::const_iterator i = line.begin();
307  ++i;
308 
309  // need space.. #if( => #if (
310  bool needSpace = true;
311  while (i != line.end()) {
312  // disable esc-mode
313  if (escapeStatus != ESC_NONE) {
314  if (prev != '\\' && escapeStatus == ESC_SINGLE && *i == '\'') {
315  escapeStatus = ESC_NONE;
316  }
317  if (prev != '\\' && escapeStatus == ESC_DOUBLE && *i == '"') {
318  escapeStatus = ESC_NONE;
319  }
320  } else {
321  // enable esc-mode
322  if (escapeStatus == ESC_NONE && *i == '"')
323  escapeStatus = ESC_DOUBLE;
324  if (escapeStatus == ESC_NONE && *i == '\'')
325  escapeStatus = ESC_SINGLE;
326  }
327  // skip double whitespace between arguments
328  if (escapeStatus == ESC_NONE && prev == ' ' && *i == ' ') {
329  ++i;
330  continue;
331  }
332  // Convert #if( to "#if ("
333  if (escapeStatus == ESC_NONE) {
334  if (needSpace) {
335  if (*i == '(' || *i == '!')
336  code << " ";
337  else if (!std::isalpha((unsigned char)*i))
338  needSpace = false;
339  }
340  if (*i == '#')
341  needSpace = true;
342  }
343  code << *i;
344  if (escapeStatus != ESC_NONE && prev == '\\' && *i == '\\') {
345  prev = ' ';
346  } else {
347  prev = *i;
348  }
349  ++i;
350  }
351  if (escapeStatus != ESC_NONE) {
352  // unmatched quotes.. compiler should probably complain about this..
353  }
354  } else {
355  // Do not mess with regular code..
356  code << line;
357  }
358  code << (sstr.eof()?"":"\n");
359  }
360 
361  return code.str();
362 }
363 
364 void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::string> &result, const std::string &filename, const std::list<std::string> &includePaths)
365 {
366  (void)includePaths;
367 
368  simplecpp::OutputList outputList;
369  std::vector<std::string> files;
370  const simplecpp::TokenList tokens1(istr, files, filename, &outputList);
371 
372 
373  const std::set<std::string> configs = getConfigs(tokens1);
374 
375  for (std::set<std::string>::const_iterator it = configs.begin(); it != configs.end(); ++it) {
376  if (_settings.userUndefs.find(*it) == _settings.userUndefs.end()) {
377  result[ *it ] = getcode(tokens1, *it, files, false);
378  }
379  }
380 }
381 
382 std::string Preprocessor::removeSpaceNearNL(const std::string &str)
383 {
384  std::string tmp;
385  char prev = '\n'; // treat start of file as newline
386  for (std::size_t i = 0; i < str.size(); i++) {
387  if (str[i] == ' ' &&
388  (prev == '\n' ||
389  i + 1 >= str.size() || // treat end of file as newline
390  str[i+1] == '\n'
391  )
392  ) {
393  // Ignore space that has new line in either side of it
394  } else {
395  tmp.append(1, str[i]);
396  prev = str[i];
397  }
398  }
399 
400  return tmp;
401 }
402 
403 void Preprocessor::preprocessWhitespaces(std::string &processedFile)
404 {
405  // Replace all tabs with spaces..
406  std::replace(processedFile.begin(), processedFile.end(), '\t', ' ');
407 
408  // Remove space characters that are after or before new line character
409  processedFile = removeSpaceNearNL(processedFile);
410 }
411 
412 void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processedFile, std::list<std::string> &resultConfigurations, const std::string &filename, const std::list<std::string> &includePaths)
413 {
414  (void)includePaths;
415 
416  if (file0.empty())
417  file0 = filename;
418 
419  simplecpp::OutputList outputList;
420  std::vector<std::string> files;
421  const simplecpp::TokenList tokens1(srcCodeStream, files, filename, &outputList);
422 
423  const std::set<std::string> configs = getConfigs(tokens1);
424  for (std::set<std::string>::const_iterator it = configs.begin(); it != configs.end(); ++it)
425  resultConfigurations.push_back(*it);
426 
427  processedFile = tokens1.stringify();
428 }
429 
430 static void splitcfg(const std::string &cfg, std::list<std::string> &defines, const std::string &defaultValue)
431 {
432  for (std::string::size_type pos1 = 0U; pos1 < cfg.size();) {
433  const std::string::size_type pos2 = cfg.find(";",pos1);
434  std::string def = (pos2 == std::string::npos) ? cfg.substr(pos1) : cfg.substr(pos1, pos2 - pos1);
435  if (!defaultValue.empty() && def.find("=") == std::string::npos)
436  def += '=' + defaultValue;
437  defines.push_back(def);
438  pos1 = (pos2 == std::string::npos) ? pos2 : pos2 + 1U;
439  }
440 }
441 
442 void Preprocessor::loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files)
443 {
444  const std::string filename(files[0]);
445 
446  // Create a map for the cfg for faster access to defines
447  simplecpp::DUI dui;
448 
449  splitcfg(_settings.userDefines, dui.defines, "1");
450 
451  for (std::vector<std::string>::const_iterator it = _settings.library.defines.begin(); it != _settings.library.defines.end(); ++it) {
452  if (it->compare(0,8,"#define ")!=0)
453  continue;
454  std::string s = it->substr(8);
455  std::string::size_type pos = s.find_first_of(" (");
456  if (pos == std::string::npos) {
457  dui.defines.push_back(s);
458  continue;
459  }
460  if (s[pos] == ' ') {
461  s[pos] = '=';
462  } else {
463  s[s.find(")")+1] = '=';
464  }
465  dui.defines.push_back(s);
466  }
467 
468  if (Path::isCPP(filename))
469  dui.defines.push_back("__cplusplus");
470 
471  dui.undefined = _settings.userUndefs;
472 
473  dui.includePaths = _settings.includePaths;
474 
475  simplecpp::OutputList outputList;
476 
477  tokenlists = simplecpp::load(rawtokens, files, dui, &outputList);
478 }
479 
481 {
482  for (std::map<std::string, simplecpp::TokenList*>::iterator it = tokenlists.begin(); it != tokenlists.end(); ++it) {
483  if (it->second)
484  it->second->removeComments();
485  }
486 }
487 
488 std::string Preprocessor::getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, const bool writeLocations)
489 {
490  const std::string filename(files[0]);
491 
492  // Create a map for the cfg for faster access to defines
493  simplecpp::DUI dui;
494 
495  splitcfg(_settings.userDefines, dui.defines, "1");
496  splitcfg(cfg, dui.defines, "");
497 
498  for (std::vector<std::string>::const_iterator it = _settings.library.defines.begin(); it != _settings.library.defines.end(); ++it) {
499  if (it->compare(0,8,"#define ")!=0)
500  continue;
501  std::string s = it->substr(8);
502  std::string::size_type pos = s.find_first_of(" (");
503  if (pos == std::string::npos) {
504  dui.defines.push_back(s);
505  continue;
506  }
507  if (s[pos] == ' ') {
508  s[pos] = '=';
509  } else {
510  s[s.find(")")+1] = '=';
511  }
512  dui.defines.push_back(s);
513  }
514 
515  if (Path::isCPP(filename))
516  dui.defines.push_back("__cplusplus");
517 
518  dui.undefined = _settings.userUndefs;
519 
520  dui.includePaths = _settings.includePaths;
521 
522  simplecpp::OutputList outputList;
523  std::list<simplecpp::MacroUsage> macroUsage;
524  simplecpp::TokenList tokens2(files);
525  simplecpp::preprocess(tokens2, tokens1, files, tokenlists, dui, &outputList, &macroUsage);
526 
527  bool showerror = (!_settings.userDefines.empty() && !_settings.force);
528  for (simplecpp::OutputList::const_iterator it = outputList.begin(); it != outputList.end(); ++it) {
529  switch (it->type) {
530  case simplecpp::Output::ERROR:
531  if (it->msg.compare(0,6,"#error")!=0 || showerror)
532  error(it->location.file(), it->location.line, it->msg);
533  return "";
534  case simplecpp::Output::WARNING:
535  break;
536  case simplecpp::Output::MISSING_INCLUDE: {
537  const std::string::size_type pos1 = it->msg.find_first_of("<\"");
538  const std::string::size_type pos2 = it->msg.find_first_of(">\"", pos1 + 1U);
539  if (pos1 < pos2 && pos2 != std::string::npos)
540  missingInclude(it->location.file(), it->location.line, it->msg.substr(pos1+1, pos2-pos1-1), it->msg[pos1] == '\"' ? UserHeader : SystemHeader);
541  }
542  break;
543  };
544  }
545 
546  // directive list..
547  directives.clear();
548  for (const simplecpp::Token *tok = tokens1.cbegin(); tok; tok = tok ? tok->next : nullptr) {
549  if ((tok->op != '#') || (tok->previous && tok->previous->location.line == tok->location.line))
550  continue;
551  if (tok->next && tok->next->str == "endfile")
552  continue;
553  Directive directive(tok->location.file(), tok->location.line, "");
554  for (const simplecpp::Token *tok2 = tok; tok2 && tok2->location.line == directive.linenr; tok2 = tok2->next) {
555  if (tok2->comment)
556  continue;
557  if (!directive.str.empty() && (tok2->location.col > tok2->previous->location.col + tok2->previous->str.size()))
558  directive.str += ' ';
559  if (directive.str == "#" && tok2->str == "file")
560  directive.str += "include";
561  else
562  directive.str += tok2->str;
563  }
564  directives.push_back(directive);
565  }
566 
567  // ensure that guessed define macros without value are not used in the code
568  for (std::list<std::string>::const_iterator defineIt = dui.defines.begin(); defineIt != dui.defines.end(); ++defineIt) {
569  if (defineIt->find("=") != std::string::npos)
570  continue;
571  const std::string macroName = defineIt->substr(0, std::min(defineIt->find("="), defineIt->find("(")));
572  for (std::list<simplecpp::MacroUsage>::const_iterator usageIt = macroUsage.begin(); usageIt != macroUsage.end(); ++usageIt) {
573  const simplecpp::MacroUsage &mu = *usageIt;
574  if (mu.macroName != macroName)
575  continue;
576  bool directiveLocation = false;
577  for (std::list<Directive>::const_iterator dirIt = directives.begin(); dirIt != directives.end(); ++dirIt) {
578  if (mu.useLocation.file() == dirIt->file && mu.useLocation.line == dirIt->linenr) {
579  directiveLocation = true;
580  break;
581  }
582  }
583  if (!directiveLocation) {
584  if (_settings.isEnabled("information"))
585  validateCfgError(cfg, macroName);
586  return "";
587  }
588  }
589  }
590 
591  // assembler code locations..
592  std::set<simplecpp::Location> assemblerLocations;
593  for (std::list<Directive>::const_iterator dirIt = directives.begin(); dirIt != directives.end(); ++dirIt) {
594  const Directive &d1 = *dirIt;
595  if (d1.str.compare(0, 11, "#pragma asm") != 0)
596  continue;
597  std::list<Directive>::const_iterator dirIt2 = dirIt;
598  ++dirIt2;
599  if (dirIt2 == directives.end())
600  continue;
601 
602  const Directive &d2 = *dirIt2;
603  if (d2.str.compare(0,14,"#pragma endasm") != 0 || d1.file != d2.file)
604  continue;
605 
606  simplecpp::Location loc(files);
607  loc.fileIndex = ~0U;
608  loc.col = 0U;
609  for (unsigned int i = 0; i < files.size(); ++i) {
610  if (files[i] == d1.file) {
611  loc.fileIndex = i;
612  break;
613  }
614  }
615 
616  for (unsigned int linenr = d1.linenr + 1U; linenr < d2.linenr; linenr++) {
617  loc.line = linenr;
618  assemblerLocations.insert(loc);
619  }
620  }
621 
622  unsigned int prevfile = 0;
623  unsigned int line = 1;
624  std::ostringstream ret;
625  for (const simplecpp::Token *tok = tokens2.cbegin(); tok; tok = tok->next) {
626  if (writeLocations && tok->location.fileIndex != prevfile) {
627  ret << "\n#line " << tok->location.line << " \"" << tok->location.file() << "\"\n";
628  prevfile = tok->location.fileIndex;
629  line = tok->location.line;
630  }
631 
632  if (tok->previous && line == tok->location.line)
633  ret << ' ';
634  bool newline = false;
635  while (tok->location.line > line) {
636  ret << '\n';
637  line++;
638  newline = true;
639  }
640  if (newline) {
641  simplecpp::Location loc = tok->location;
642  loc.col = 0U;
643  if (assemblerLocations.find(loc) != assemblerLocations.end()) {
644  ret << "asm();";
645  while (assemblerLocations.find(loc) != assemblerLocations.end()) {
646  loc.line++;
647  }
648  while (tok && tok->location.line < loc.line)
649  tok = tok->next;
650  if (!tok)
651  break;
652  while (line < tok->location.line) {
653  ret << '\n';
654  ++line;
655  }
656  }
657  }
658  if (!tok->macro.empty())
660  ret << tok->str;
661  }
662 
663  return ret.str();
664 }
665 
666 std::string Preprocessor::getcode(const std::string &filedata, const std::string &cfg, const std::string &filename)
667 {
668  simplecpp::OutputList outputList;
669  std::vector<std::string> files;
670 
671  std::istringstream istr(filedata);
672  const simplecpp::TokenList &tokens1 = simplecpp::TokenList(istr, files, Path::simplifyPath(filename), &outputList);
673  inlineSuppressions(tokens1);
674 
675  for (simplecpp::OutputList::const_iterator it = outputList.begin(); it != outputList.end(); ++it) {
676  switch (it->type) {
677  case simplecpp::Output::ERROR:
678  error(it->location.file(), it->location.line, it->msg);
679  return "";
680  case simplecpp::Output::WARNING:
681  break;
682  case simplecpp::Output::MISSING_INCLUDE: {
683  const std::string::size_type pos1 = it->msg.find_first_of("<\"");
684  const std::string::size_type pos2 = it->msg.find_first_of(">\"", pos1 + 1U);
685  if (pos1 < pos2 && pos2 != std::string::npos)
686  missingInclude(it->location.file(), it->location.line, it->msg.substr(pos1+1, pos2-pos1-1), it->msg[pos1] == '\"' ? UserHeader : SystemHeader);
687  }
688  break;
689  };
690  }
691 
692  return getcode(tokens1, cfg, files, filedata.find("#file") != std::string::npos);
693 }
694 
695 void Preprocessor::error(const std::string &filename, unsigned int linenr, const std::string &msg)
696 {
697  std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
698  if (!filename.empty()) {
699  ErrorLogger::ErrorMessage::FileLocation loc(filename, linenr);
700  locationList.push_back(loc);
701  }
704  msg,
705  "preprocessorErrorDirective",
706  false));
707 }
708 
710 {
711  std::string::size_type i = str.find_first_of("<\"");
712  if (i == std::string::npos) {
713  str = "";
714  return NoHeader;
715  }
716 
717  char c = str[i];
718  if (c == '<')
719  c = '>';
720 
721  std::string result;
722  for (i = i + 1; i < str.length(); ++i) {
723  if (str[i] == c)
724  break;
725 
726  result.append(1, str[i]);
727  }
728 
729  // Linux can't open include paths with \ separator, so fix them
730  std::replace(result.begin(), result.end(), '\\', '/');
731 
732  str = result;
733 
734  return (c == '\"') ? UserHeader : SystemHeader;
735 }
736 
737 
738 // Report that include is missing
739 void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType)
740 {
741  const std::string fname = Path::fromNativeSeparators(filename);
742  if (_settings.nomsg.isSuppressed("missingInclude", fname, linenr))
743  return;
744  if (headerType == SystemHeader && _settings.nomsg.isSuppressed("missingIncludeSystem", fname, linenr))
745  return;
746 
747  if (headerType == SystemHeader)
749  else
750  missingIncludeFlag = true;
752 
753  std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
754  if (!filename.empty()) {
756  loc.line = linenr;
757  loc.setfile(Path::toNativeSeparators(filename));
758  locationList.push_back(loc);
759  }
761  (headerType==SystemHeader) ?
762  "Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results." :
763  "Include file: \"" + header + "\" not found.",
764  (headerType==SystemHeader) ? "missingIncludeSystem" : "missingInclude",
765  false);
766  errmsg.file0 = file0;
767  _errorLogger->reportInfo(errmsg);
768  }
769 }
770 
771 bool Preprocessor::validateCfg(const std::string &code, const std::string &cfg)
772 {
773  const bool printInformation = _settings.isEnabled("information");
774 
775  // fill up "macros" with empty configuration macros
776  std::set<std::string> macros;
777  for (std::string::size_type pos = 0; pos < cfg.size();) {
778  const std::string::size_type pos2 = cfg.find_first_of(";=", pos);
779  if (pos2 == std::string::npos) {
780  macros.insert(cfg.substr(pos));
781  break;
782  }
783  if (cfg[pos2] == ';')
784  macros.insert(cfg.substr(pos, pos2-pos));
785  pos = cfg.find(';', pos2);
786  if (pos != std::string::npos)
787  ++pos;
788  }
789 
790  // check if any empty macros are used in code
791  for (std::set<std::string>::const_iterator it = macros.begin(); it != macros.end(); ++it) {
792  const std::string &macro = *it;
793  std::string::size_type pos = 0;
794  while ((pos = code.find_first_of(std::string("#\"'")+macro[0], pos)) != std::string::npos) {
795  const std::string::size_type pos1 = pos;
796  const std::string::size_type pos2 = pos + macro.size();
797  pos++;
798 
799  // skip string..
800  if (code[pos1] == '\"' || code[pos1] == '\'') {
801  while (pos < code.size() && code[pos] != code[pos1]) {
802  if (code[pos] == '\\')
803  ++pos;
804  ++pos;
805  }
806  ++pos;
807  }
808 
809  // skip preprocessor statement..
810  else if (code[pos1] == '#') {
811  if (pos1 == 0 || code[pos1-1] == '\n')
812  pos = code.find('\n', pos);
813  }
814 
815  // is macro used in code?
816  else if (code.compare(pos1,macro.size(),macro) == 0) {
817  if (pos1 > 0 && (std::isalnum((unsigned char)code[pos1-1U]) || code[pos1-1U] == '_'))
818  continue;
819  if (pos2 < code.size() && (std::isalnum((unsigned char)code[pos2]) || code[pos2] == '_'))
820  continue;
821  // macro is used in code, return false
822  if (printInformation)
823  validateCfgError(cfg, macro);
824  return false;
825  }
826  }
827  }
828 
829  return true;
830 }
831 
832 void Preprocessor::validateCfgError(const std::string &cfg, const std::string &macro)
833 {
834  const std::string id = "ConfigurationNotChecked";
835  std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
837  locationList.push_back(loc);
838  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);
839  _errorLogger->reportInfo(errmsg);
840 }
841 
842 void Preprocessor::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
843 {
844  Settings settings2(*settings);
845  Preprocessor preprocessor(settings2, errorLogger);
846  settings2.checkConfiguration=true;
847  preprocessor.missingInclude("", 1, "", UserHeader);
848  preprocessor.missingInclude("", 1, "", SystemHeader);
849  preprocessor.validateCfgError("X", "X");
850  preprocessor.error("", 1, "#error message"); // #error ..
851 }
852 
853 void Preprocessor::dump(std::ostream &out) const
854 {
855  // Create a xml directive dump.
856  // The idea is not that this will be readable for humans. It's a
857  // data dump that 3rd party tools could load and get useful info from.
858  std::list<Directive>::const_iterator it;
859 
860  out << " <directivelist>" << std::endl;
861 
862  for (it = directives.begin(); it != directives.end(); ++it) {
863  out << " <directive "
864  << "file=\"" << it->file << "\" "
865  << "linenr=\"" << it->linenr << "\" "
866  // str might contain characters such as '"', '<' or '>' which
867  // could result in invalid XML, so run it through toxml().
868  << "str=\"" << ErrorLogger::toxml(it->str) << "\"/>" << std::endl;
869  }
870  out << " </directivelist>" << std::endl;
871 }