MCPcopy Create free account
hub / github.com/KDAB/codebrowser / MacroExpands

Method MacroExpands

generator/preprocessorcallback.cpp:33–169  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31
32
33void PreprocessorCallback::MacroExpands(const clang::Token& MacroNameTok,
34 MyMacroDefinition MD,
35 clang::SourceRange Range, const clang::MacroArgs *)
36{
37 if (disabled)
38 return;
39
40#if CLANG_VERSION_MAJOR != 3 || CLANG_VERSION_MINOR >= 7
41 auto *MI = MD.getMacroInfo();
42#else
43 auto *MI = MD->getMacroInfo();
44#endif
45 clang::SourceLocation loc = MacroNameTok.getLocation();
46 if (!loc.isValid() || !loc.isFileID())
47 return;
48 clang::SourceManager &sm = annotator.getSourceMgr();
49 clang::FileID FID = sm.getFileID(loc);
50 if (!annotator.shouldProcess(FID))
51 return;
52
53 const char *begin = sm.getCharacterData(Range.getBegin());
54 int len = sm.getCharacterData(Range.getEnd()) - begin;
55 len += clang::Lexer::MeasureTokenLength(Range.getEnd(), sm, PP.getLangOpts());
56
57 std::string copy(begin, len);
58 begin = copy.c_str();
59 clang::Lexer lex(loc, PP.getLangOpts(), begin, begin, begin + len);
60 std::vector<clang::Token> tokens;
61 std::string expansion;
62
63 //Lousely based on code from clang::html::HighlightMacros
64
65 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
66 // macros.
67 clang::Token tok;
68 do {
69 lex.LexFromRawLexer(tok);
70
71 // If this is a # at the start of a line, discard it from the token stream.
72 // We don't want the re-preprocess step to see #defines, #includes or other
73 // preprocessor directives.
74 if (tok.is(clang::tok::hash) && tok.isAtStartOfLine())
75 continue;
76
77 // If this is a ## token, change its kind to unknown so that repreprocessing
78 // it will not produce an error.
79 if (tok.is(clang::tok::hashhash))
80 tok.setKind(clang::tok::unknown);
81
82 // If this raw token is an identifier, the raw lexer won't have looked up
83 // the corresponding identifier info for it. Do this now so that it will be
84 // macro expanded when we re-preprocess it.
85 if (tok.is(clang::tok::raw_identifier))
86 PP.LookUpIdentifierInfo(tok);
87
88 tokens.push_back(tok);
89
90 } while(!tok.is(clang::tok::eof));

Callers

nothing calls this directly

Calls 6

TwineClass · 0.85
pathToMethod · 0.80
addTagMethod · 0.80
registerMacroMethod · 0.80
shouldProcessMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected