Coverage Report

Created: 2023-11-11 10:31

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Analysis/MacroExpansionContext.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MacroExpansionContext.cpp - Macro expansion information --*- C++ -*-===//
2
//
3
// Part of the LLVM Project, 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
#include "clang/Analysis/MacroExpansionContext.h"
10
#include "llvm/Support/Debug.h"
11
#include <optional>
12
13
#define DEBUG_TYPE "macro-expansion-context"
14
15
static void dumpTokenInto(const clang::Preprocessor &PP, clang::raw_ostream &OS,
16
                          clang::Token Tok);
17
18
namespace clang {
19
namespace detail {
20
class MacroExpansionRangeRecorder : public PPCallbacks {
21
  const Preprocessor &PP;
22
  SourceManager &SM;
23
  MacroExpansionContext::ExpansionRangeMap &ExpansionRanges;
24
25
public:
26
  explicit MacroExpansionRangeRecorder(
27
      const Preprocessor &PP, SourceManager &SM,
28
      MacroExpansionContext::ExpansionRangeMap &ExpansionRanges)
29
14
      : PP(PP), SM(SM), ExpansionRanges(ExpansionRanges) {}
30
31
  void MacroExpands(const Token &MacroName, const MacroDefinition &MD,
32
140
                    SourceRange Range, const MacroArgs *Args) override {
33
    // Ignore annotation tokens like: _Pragma("pack(push, 1)")
34
140
    if (MacroName.getIdentifierInfo()->getName() == "_Pragma")
35
2
      return;
36
37
138
    SourceLocation MacroNameBegin = SM.getExpansionLoc(MacroName.getLocation());
38
138
    assert(MacroNameBegin == SM.getExpansionLoc(Range.getBegin()));
39
40
138
    const SourceLocation ExpansionEnd = [Range, &SM = SM, &MacroName] {
41
      // If the range is empty, use the length of the macro.
42
138
      if (Range.getBegin() == Range.getEnd())
43
58
        return SM.getExpansionLoc(
44
58
            MacroName.getLocation().getLocWithOffset(MacroName.getLength()));
45
46
      // Include the last character.
47
80
      return SM.getExpansionLoc(Range.getEnd()).getLocWithOffset(1);
48
138
    }();
49
50
138
    (void)PP;
51
138
    LLVM_DEBUG(llvm::dbgs() << "MacroExpands event: '";
52
138
               dumpTokenInto(PP, llvm::dbgs(), MacroName);
53
138
               llvm::dbgs()
54
138
               << "' with length " << MacroName.getLength() << " at ";
55
138
               MacroNameBegin.print(llvm::dbgs(), SM);
56
138
               llvm::dbgs() << ", expansion end at ";
57
138
               ExpansionEnd.print(llvm::dbgs(), SM); llvm::dbgs() << '\n';);
58
59
    // If the expansion range is empty, use the identifier of the macro as a
60
    // range.
61
138
    MacroExpansionContext::ExpansionRangeMap::iterator It;
62
138
    bool Inserted;
63
138
    std::tie(It, Inserted) =
64
138
        ExpansionRanges.try_emplace(MacroNameBegin, ExpansionEnd);
65
138
    if (Inserted) {
66
77
      LLVM_DEBUG(llvm::dbgs() << "maps ";
67
77
                 It->getFirst().print(llvm::dbgs(), SM); llvm::dbgs() << " to ";
68
77
                 It->getSecond().print(llvm::dbgs(), SM);
69
77
                 llvm::dbgs() << '\n';);
70
77
    } else {
71
61
      if (SM.isBeforeInTranslationUnit(It->getSecond(), ExpansionEnd)) {
72
3
        It->getSecond() = ExpansionEnd;
73
3
        LLVM_DEBUG(
74
3
            llvm::dbgs() << "remaps "; It->getFirst().print(llvm::dbgs(), SM);
75
3
            llvm::dbgs() << " to "; It->getSecond().print(llvm::dbgs(), SM);
76
3
            llvm::dbgs() << '\n';);
77
3
      }
78
61
    }
79
138
  }
80
};
81
} // namespace detail
82
} // namespace clang
83
84
using namespace clang;
85
86
MacroExpansionContext::MacroExpansionContext(const LangOptions &LangOpts)
87
1.81k
    : LangOpts(LangOpts) {}
88
89
14
void MacroExpansionContext::registerForPreprocessor(Preprocessor &NewPP) {
90
14
  PP = &NewPP;
91
14
  SM = &NewPP.getSourceManager();
92
93
  // Make sure that the Preprocessor does not outlive the MacroExpansionContext.
94
14
  PP->addPPCallbacks(std::make_unique<detail::MacroExpansionRangeRecorder>(
95
14
      *PP, *SM, ExpansionRanges));
96
  // Same applies here.
97
1.47k
  PP->setTokenWatcher([this](const Token &Tok) { onTokenLexed(Tok); });
98
14
}
99
100
std::optional<StringRef>
101
82
MacroExpansionContext::getExpandedText(SourceLocation MacroExpansionLoc) const {
102
82
  if (MacroExpansionLoc.isMacroID())
103
0
    return std::nullopt;
104
105
  // If there was no macro expansion at that location, return std::nullopt.
106
82
  if (ExpansionRanges.find_as(MacroExpansionLoc) == ExpansionRanges.end())
107
15
    return std::nullopt;
108
109
  // There was macro expansion, but resulted in no tokens, return empty string.
110
67
  const auto It = ExpandedTokens.find_as(MacroExpansionLoc);
111
67
  if (It == ExpandedTokens.end())
112
7
    return StringRef{""};
113
114
  // Otherwise we have the actual token sequence as string.
115
60
  return It->getSecond().str();
116
67
}
117
118
std::optional<StringRef>
119
83
MacroExpansionContext::getOriginalText(SourceLocation MacroExpansionLoc) const {
120
83
  if (MacroExpansionLoc.isMacroID())
121
0
    return std::nullopt;
122
123
83
  const auto It = ExpansionRanges.find_as(MacroExpansionLoc);
124
83
  if (It == ExpansionRanges.end())
125
15
    return std::nullopt;
126
127
68
  assert(It->getFirst() != It->getSecond() &&
128
68
         "Every macro expansion must cover a non-empty range.");
129
130
68
  return Lexer::getSourceText(
131
68
      CharSourceRange::getCharRange(It->getFirst(), It->getSecond()), *SM,
132
68
      LangOpts);
133
68
}
134
135
0
void MacroExpansionContext::dumpExpansionRanges() const {
136
0
  dumpExpansionRangesToStream(llvm::dbgs());
137
0
}
138
0
void MacroExpansionContext::dumpExpandedTexts() const {
139
0
  dumpExpandedTextsToStream(llvm::dbgs());
140
0
}
141
142
1
void MacroExpansionContext::dumpExpansionRangesToStream(raw_ostream &OS) const {
143
1
  std::vector<std::pair<SourceLocation, SourceLocation>> LocalExpansionRanges;
144
1
  LocalExpansionRanges.reserve(ExpansionRanges.size());
145
1
  for (const auto &Record : ExpansionRanges)
146
0
    LocalExpansionRanges.emplace_back(
147
0
        std::make_pair(Record.getFirst(), Record.getSecond()));
148
1
  llvm::sort(LocalExpansionRanges);
149
150
1
  OS << "\n=============== ExpansionRanges ===============\n";
151
1
  for (const auto &Record : LocalExpansionRanges) {
152
0
    OS << "> ";
153
0
    Record.first.print(OS, *SM);
154
0
    OS << ", ";
155
0
    Record.second.print(OS, *SM);
156
0
    OS << '\n';
157
0
  }
158
1
}
159
160
1
void MacroExpansionContext::dumpExpandedTextsToStream(raw_ostream &OS) const {
161
1
  std::vector<std::pair<SourceLocation, MacroExpansionText>>
162
1
      LocalExpandedTokens;
163
1
  LocalExpandedTokens.reserve(ExpandedTokens.size());
164
1
  for (const auto &Record : ExpandedTokens)
165
0
    LocalExpandedTokens.emplace_back(
166
0
        std::make_pair(Record.getFirst(), Record.getSecond()));
167
1
  llvm::sort(LocalExpandedTokens);
168
169
1
  OS << "\n=============== ExpandedTokens ===============\n";
170
1
  for (const auto &Record : LocalExpandedTokens) {
171
0
    OS << "> ";
172
0
    Record.first.print(OS, *SM);
173
0
    OS << " -> '" << Record.second << "'\n";
174
0
  }
175
1
}
176
177
525
static void dumpTokenInto(const Preprocessor &PP, raw_ostream &OS, Token Tok) {
178
525
  assert(Tok.isNot(tok::raw_identifier));
179
180
  // Ignore annotation tokens like: _Pragma("pack(push, 1)")
181
525
  if (Tok.isAnnotation())
182
0
    return;
183
184
525
  if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
185
    // FIXME: For now, we don't respect whitespaces between macro expanded
186
    // tokens. We just emit a space after every identifier to produce a valid
187
    // code for `int a ;` like expansions.
188
    //              ^-^-- Space after the 'int' and 'a' identifiers.
189
157
    OS << II->getName() << ' ';
190
368
  } else if (Tok.isLiteral() && 
!Tok.needsCleaning()64
&&
Tok.getLiteralData()64
) {
191
64
    OS << StringRef(Tok.getLiteralData(), Tok.getLength());
192
304
  } else {
193
304
    char Tmp[256];
194
304
    if (Tok.getLength() < sizeof(Tmp)) {
195
304
      const char *TokPtr = Tmp;
196
      // FIXME: Might use a different overload for cleaner callsite.
197
304
      unsigned Len = PP.getSpelling(Tok, TokPtr);
198
304
      OS.write(TokPtr, Len);
199
304
    } else {
200
0
      OS << "<too long token>";
201
0
    }
202
304
  }
203
525
}
204
205
1.47k
void MacroExpansionContext::onTokenLexed(const Token &Tok) {
206
1.47k
  SourceLocation SLoc = Tok.getLocation();
207
1.47k
  if (SLoc.isFileID())
208
950
    return;
209
210
525
  LLVM_DEBUG(llvm::dbgs() << "lexed macro expansion token '";
211
525
             dumpTokenInto(*PP, llvm::dbgs(), Tok); llvm::dbgs() << "' at ";
212
525
             SLoc.print(llvm::dbgs(), *SM); llvm::dbgs() << '\n';);
213
214
  // Remove spelling location.
215
525
  SourceLocation CurrExpansionLoc = SM->getExpansionLoc(SLoc);
216
217
525
  MacroExpansionText TokenAsString;
218
525
  llvm::raw_svector_ostream OS(TokenAsString);
219
220
  // FIXME: Prepend newlines and space to produce the exact same output as the
221
  // preprocessor would for this token.
222
223
525
  dumpTokenInto(*PP, OS, Tok);
224
225
525
  ExpansionMap::iterator It;
226
525
  bool Inserted;
227
525
  std::tie(It, Inserted) =
228
525
      ExpandedTokens.try_emplace(CurrExpansionLoc, std::move(TokenAsString));
229
525
  if (!Inserted)
230
463
    It->getSecond().append(TokenAsString);
231
525
}
232