MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / splitDefinitionFromTypedef

Function splitDefinitionFromTypedef

lib/tokenize.cpp:364–431  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

362}
363
364static Token *splitDefinitionFromTypedef(Token *tok, nonneg int *unnamedCount)
365{
366 std::string name;
367 std::set<std::string> qualifiers;
368
369 while (Token::Match(tok->next(), "const|volatile")) {
370 qualifiers.insert(tok->strAt(1));
371 tok->deleteNext();
372 }
373
374 // skip "class|struct|union|enum"
375 Token *tok1 = tok->tokAt(2);
376
377 const bool hasName = Token::Match(tok1, "%name%");
378
379 // skip name
380 if (hasName) {
381 name = tok1->str();
382 tok1 = tok1->next();
383 }
384
385 // skip base classes if present
386 if (tok1->str() == ":") {
387 tok1 = tok1->next();
388 while (tok1 && tok1->str() != "{")
389 tok1 = tok1->next();
390 if (!tok1)
391 return nullptr;
392 }
393
394 // skip to end
395 tok1 = tok1->link();
396
397 if (!hasName) { // unnamed
398 if (tok1->next()) {
399 // use typedef name if available
400 if (Token::Match(tok1->next(), "%type%"))
401 name = tok1->strAt(1);
402 else // create a unique name
403 name = "Unnamed" + std::to_string((*unnamedCount)++);
404 tok->next()->insertToken(name);
405 } else
406 return nullptr;
407 }
408
409 tok1->insertToken(";");
410 tok1 = tok1->next();
411
412 if (tok1->next() && tok1->strAt(1) == ";" && tok1->strAt(-1) == "}") {
413 tok->deleteThis();
414 tok1->deleteThis();
415 return nullptr;
416 }
417 tok1->insertToken("typedef");
418 tok1 = tok1->next();
419 Token * tok3 = tok1;
420 for (const std::string &qualifier : qualifiers) {
421 tok1->insertToken(qualifier);

Callers 1

simplifyTypedefCppMethod · 0.85

Calls 6

nextMethod · 0.80
deleteNextMethod · 0.80
insertTokenMethod · 0.80
deleteThisMethod · 0.80
tokAtMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected