Debian Codegraph @

libcextract/MacroWalker.cpp

raw
1 //===- MacroWalker.cpp - Get macro structures according to location *- C++ -*-===//
2 //
3 // This project is licensed under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Helper functions and methods to handle macros.
11 //
12 //===----------------------------------------------------------------------===//
13
14 /* Author: Giuliano Belinassi */
15
16 #include "MacroWalker.hh"
17 #include "PrettyPrint.hh"
18
19 MacroInfo *MacroWalker::Get_Macro_Info(MacroDefinitionRecord *record)
20 {
21 MacroDirective *directive = Get_Macro_Directive(record);
22 return directive ? directive->getMacroInfo() : nullptr;
23 }
24
25 MacroInfo *MacroWalker::Get_Macro_Info(MacroExpansion *macroexp)
26 {
27 SourceLocation loc = macroexp->getSourceRange().getBegin();
28 const IdentifierInfo *id = macroexp->getName();
29 return Get_Macro_Info(id, loc);
30 }
31
32 MacroInfo *MacroWalker::Get_Macro_Info(const IdentifierInfo *id, const SourceLocation &loc)
33 {
34 MacroDirective *directive = PProcessor.getLocalMacroDirectiveHistory(id);
35 while (directive) {
36 MacroInfo *macroinfo = directive->getMacroInfo();
37
38 if (!macroinfo->getDefinitionLoc().isValid()) {
39 /* Skip if macro expansion seems to not have a valid location. */
40 directive = directive->getPrevious();
41 continue;
42 }
43
44 /* If this MacroInfo object location is defined AFTER the given loc, then
45 it means we are looking for a macro that was later redefined. So we
46 look into the previous definitions to find the last one that is defined
47 before loc. */
48
49 if (PrettyPrint::Is_Before(macroinfo->getDefinitionLoc(), loc))
50 return macroinfo;
51
52 directive = directive->getPrevious();
53 }
54
55 /* MacroInfo object not found. */
56 return nullptr;
57 }
58
59 MacroDirective *MacroWalker::Get_Macro_Directive(MacroDefinitionRecord *record)
60 {
61 const IdentifierInfo *id = record->getName();
62 MacroDirective *directive = PProcessor.getLocalMacroDirectiveHistory(id);
63 while (directive) {
64 MacroInfo *macroinfo = directive->getMacroInfo();
65
66 /* If this MacroInfo object location is defined OUTSIDE the macro defintion,
67 then it means we are looking for a macro that was later redefined. */
68 SourceRange range1(macroinfo->getDefinitionLoc(), macroinfo->getDefinitionEndLoc());
69 SourceRange range2(record->getLocation());
70 if (range1.fullyContains(range2)) {
71 return directive;
72 }
73 directive = directive->getPrevious();
74 }
75
76 return nullptr;
77 }
78
79 bool MacroWalker::Is_Builtin_Macro(MacroInfo *info)
80 {
81 if (info->isBuiltinMacro())
82 return true;
83
84 /* Some cases the isBuiltinMacro method fails on builtin macros. Try to
85 decide it using the FileInfo. */
86 OptionalFileEntryRef ref = PrettyPrint::Get_FileEntry(info->getDefinitionLoc());
87 if (!ref.has_value()) {
88 /* This doesn't come from any file, hence it was introduced by the compiler,
89 therefore is a builtin macro. */
90 return true;
91 }
92
93 /* This comes from a file and therefore is not a builtin macro. */
94 return false;
95 }
96
97 bool MacroWalker::Is_Identifier_Macro_Argument(MacroInfo *info, StringRef tok_str)
98 {
99 for (const IdentifierInfo *arg : info->params()) {
100 StringRef arg_str = arg->getName();
101 if (tok_str == arg_str) {
102 return true;
103 }
104 }
105
106 return false;
107 }
108
109 bool MacroWalker::Is_Identifier_Macro_Argument(MacroInfo *info, const IdentifierInfo *tok)
110 {
111 if (tok == nullptr)
112 return false;
113
114 return Is_Identifier_Macro_Argument(info, tok->getName());
115 }