clang-tools 22.0.0git
MacroToEnumCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 "MacroToEnumCheck.h"
11
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Lex/Preprocessor.h"
15#include "llvm/ADT/STLExtras.h"
16#include <cassert>
17#include <cctype>
18#include <string>
19
20namespace clang::tidy::modernize {
21
22static bool hasOnlyComments(SourceLocation Loc, const LangOptions &Options,
23 StringRef Text) {
24 // Use a lexer to look for tokens; if we find something other than a single
25 // hash, then there were intervening tokens between macro definitions.
26 const std::string Buffer{Text};
27 Lexer Lex(Loc, Options, Buffer.c_str(), Buffer.c_str(),
28 Buffer.c_str() + Buffer.size());
29 Token Tok;
30 bool SeenHash = false;
31 while (!Lex.LexFromRawLexer(Tok)) {
32 if (Tok.getKind() == tok::hash && !SeenHash) {
33 SeenHash = true;
34 continue;
35 }
36 return false;
37 }
38
39 // Everything in between was whitespace, so now just look for two blank lines,
40 // consisting of two consecutive EOL sequences, either '\n', '\r' or '\r\n'.
41 enum class WhiteSpace {
42 Nothing,
43 CR,
44 LF,
45 CRLF,
46 CRLFCR,
47 };
48
49 WhiteSpace State = WhiteSpace::Nothing;
50 for (const char C : Text) {
51 switch (C) {
52 case '\r':
53 if (State == WhiteSpace::CR)
54 return false;
55
56 State = State == WhiteSpace::CRLF ? WhiteSpace::CRLFCR : WhiteSpace::CR;
57 break;
58
59 case '\n':
60 if (State == WhiteSpace::LF || State == WhiteSpace::CRLFCR)
61 return false;
62
63 State = State == WhiteSpace::CR ? WhiteSpace::CRLF : WhiteSpace::LF;
64 break;
65
66 default:
67 State = WhiteSpace::Nothing;
68 break;
69 }
70 }
71
72 return true;
73}
74
75static StringRef getTokenName(const Token &Tok) {
76 return Tok.is(tok::raw_identifier) ? Tok.getRawIdentifier()
77 : Tok.getIdentifierInfo()->getName();
78}
79
80namespace {
81
82struct EnumMacro {
83 EnumMacro(Token Name, const MacroDirective *Directive)
84 : Name(Name), Directive(Directive) {}
85
86 Token Name;
87 const MacroDirective *Directive;
88};
89
90using MacroList = SmallVector<EnumMacro>;
91
92enum class IncludeGuard { None, FileChanged, IfGuard, DefineGuard };
93
94struct FileState {
95 FileState() = default;
96
97 int ConditionScopes = 0;
98 unsigned int LastLine = 0;
99 IncludeGuard GuardScanner = IncludeGuard::None;
100 SourceLocation LastMacroLocation;
101};
102
103} // namespace
104
105class MacroToEnumCallbacks : public PPCallbacks {
106public:
107 MacroToEnumCallbacks(MacroToEnumCheck *Check, const LangOptions &LangOptions,
108 const SourceManager &SM)
109 : Check(Check), LangOpts(LangOptions), SM(SM) {}
110
111 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
112 SrcMgr::CharacteristicKind FileType,
113 FileID PrevFID) override;
114
115 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
116 StringRef FileName, bool IsAngled,
117 CharSourceRange FilenameRange,
118 OptionalFileEntryRef File, StringRef SearchPath,
119 StringRef RelativePath, const Module *SuggestedModule,
120 bool ModuleImported,
121 SrcMgr::CharacteristicKind FileType) override {
122 clearCurrentEnum(HashLoc);
123 }
124
125 // Keep track of macro definitions that look like enums.
126 void MacroDefined(const Token &MacroNameTok,
127 const MacroDirective *MD) override;
128
129 // Undefining an enum-like macro results in the enum set being dropped.
130 void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
131 const MacroDirective *Undef) override;
132
133 // Conditional compilation clears any adjacent enum-like macros.
134 // Macros used in conditional expressions clear any adjacent enum-like
135 // macros.
136 // Include guards are either
137 // #if !defined(GUARD)
138 // or
139 // #ifndef GUARD
140 void If(SourceLocation Loc, SourceRange ConditionRange,
141 ConditionValueKind ConditionValue) override {
142 conditionStart(Loc);
143 checkCondition(ConditionRange);
144 }
145 void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
146 const MacroDefinition &MD) override {
147 conditionStart(Loc);
148 checkName(MacroNameTok);
149 }
150 void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
151 const MacroDefinition &MD) override {
152 conditionStart(Loc);
153 checkName(MacroNameTok);
154 }
155 void Elif(SourceLocation Loc, SourceRange ConditionRange,
156 ConditionValueKind ConditionValue, SourceLocation IfLoc) override {
157 checkCondition(ConditionRange);
158 }
159 void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
160 const MacroDefinition &MD) override {
161 checkName(MacroNameTok);
162 }
163 void Elifdef(SourceLocation Loc, SourceRange ConditionRange,
164 SourceLocation IfLoc) override {
165 PPCallbacks::Elifdef(Loc, ConditionRange, IfLoc);
166 }
167 void Elifndef(SourceLocation Loc, const Token &MacroNameTok,
168 const MacroDefinition &MD) override {
169 checkName(MacroNameTok);
170 }
171 void Elifndef(SourceLocation Loc, SourceRange ConditionRange,
172 SourceLocation IfLoc) override {
173 PPCallbacks::Elifndef(Loc, ConditionRange, IfLoc);
174 }
175 void Endif(SourceLocation Loc, SourceLocation IfLoc) override;
176 void PragmaDirective(SourceLocation Loc,
177 PragmaIntroducerKind Introducer) override;
178
179 // After we've seen everything, issue warnings and fix-its.
180 void EndOfMainFile() override;
181
182 void invalidateRange(SourceRange Range);
183
184private:
185 void newEnum() {
186 if (Enums.empty() || !Enums.back().empty())
187 Enums.emplace_back();
188 }
189 bool insideConditional() const {
190 return (CurrentFile->GuardScanner == IncludeGuard::DefineGuard &&
191 CurrentFile->ConditionScopes > 1) ||
192 (CurrentFile->GuardScanner != IncludeGuard::DefineGuard &&
193 CurrentFile->ConditionScopes > 0);
194 }
195 bool isConsecutiveMacro(const MacroDirective *MD) const;
196 void rememberLastMacroLocation(const MacroDirective *MD) {
197 CurrentFile->LastLine = SM.getSpellingLineNumber(MD->getLocation());
198 CurrentFile->LastMacroLocation = Lexer::getLocForEndOfToken(
199 MD->getMacroInfo()->getDefinitionEndLoc(), 0, SM, LangOpts);
200 }
201 void clearLastMacroLocation() {
202 CurrentFile->LastLine = 0;
203 CurrentFile->LastMacroLocation = SourceLocation{};
204 }
205 void clearCurrentEnum(SourceLocation Loc);
206 void conditionStart(const SourceLocation &Loc);
207 void checkCondition(SourceRange ConditionRange);
208 void checkName(const Token &MacroNameTok);
209 void rememberExpressionName(const Token &Tok);
210 void rememberExpressionTokens(ArrayRef<Token> MacroTokens);
211 void invalidateExpressionNames();
212 void issueDiagnostics();
213 void warnMacroEnum(const EnumMacro &Macro) const;
214 void fixEnumMacro(const MacroList &MacroList) const;
215 bool isInitializer(ArrayRef<Token> MacroTokens);
216
217 MacroToEnumCheck *Check;
218 const LangOptions &LangOpts;
219 const SourceManager &SM;
220 SmallVector<MacroList> Enums;
221 SmallVector<FileState> Files;
222 std::vector<std::string> ExpressionNames;
223 FileState *CurrentFile = nullptr;
224};
225
226bool MacroToEnumCallbacks::isConsecutiveMacro(const MacroDirective *MD) const {
227 if (CurrentFile->LastMacroLocation.isInvalid())
228 return false;
229
230 const SourceLocation Loc = MD->getLocation();
231 if (CurrentFile->LastLine + 1 == SM.getSpellingLineNumber(Loc))
232 return true;
233
234 const SourceLocation Define =
235 SM.translateLineCol(SM.getFileID(Loc), SM.getSpellingLineNumber(Loc), 1);
236 const CharSourceRange BetweenMacros{
237 SourceRange{CurrentFile->LastMacroLocation, Define}, true};
238 const CharSourceRange CharRange =
239 Lexer::makeFileCharRange(BetweenMacros, SM, LangOpts);
240 const StringRef BetweenText = Lexer::getSourceText(CharRange, SM, LangOpts);
241 return hasOnlyComments(Define, LangOpts, BetweenText);
242}
243
244void MacroToEnumCallbacks::clearCurrentEnum(SourceLocation Loc) {
245 // Only drop the most recent Enum set if the directive immediately follows.
246 if (!Enums.empty() && !Enums.back().empty() &&
247 SM.getSpellingLineNumber(Loc) == CurrentFile->LastLine + 1)
248 Enums.pop_back();
249
250 clearLastMacroLocation();
251}
252
253void MacroToEnumCallbacks::conditionStart(const SourceLocation &Loc) {
254 ++CurrentFile->ConditionScopes;
255 clearCurrentEnum(Loc);
256 if (CurrentFile->GuardScanner == IncludeGuard::FileChanged)
257 CurrentFile->GuardScanner = IncludeGuard::IfGuard;
258}
259
260void MacroToEnumCallbacks::checkCondition(SourceRange Range) {
261 const CharSourceRange CharRange = Lexer::makeFileCharRange(
262 CharSourceRange::getTokenRange(Range), SM, LangOpts);
263 std::string Text = Lexer::getSourceText(CharRange, SM, LangOpts).str();
264 Lexer Lex(CharRange.getBegin(), LangOpts, Text.data(), Text.data(),
265 Text.data() + Text.size());
266 Token Tok;
267 bool End = false;
268 while (!End) {
269 End = Lex.LexFromRawLexer(Tok);
270 if (Tok.is(tok::raw_identifier) &&
271 Tok.getRawIdentifier().str() != "defined")
272 checkName(Tok);
273 }
274}
275
276void MacroToEnumCallbacks::checkName(const Token &MacroNameTok) {
277 rememberExpressionName(MacroNameTok);
278
279 StringRef Id = getTokenName(MacroNameTok);
280 llvm::erase_if(Enums, [&Id](const MacroList &MacroList) {
281 return llvm::any_of(MacroList, [&Id](const EnumMacro &Macro) {
282 return getTokenName(Macro.Name) == Id;
283 });
284 });
285}
286
287void MacroToEnumCallbacks::rememberExpressionName(const Token &Tok) {
288 const std::string Id = getTokenName(Tok).str();
289 auto Pos = llvm::lower_bound(ExpressionNames, Id);
290 if (Pos == ExpressionNames.end() || *Pos != Id)
291 ExpressionNames.insert(Pos, Id);
292}
293
294void MacroToEnumCallbacks::rememberExpressionTokens(
295 ArrayRef<Token> MacroTokens) {
296 for (const Token Tok : MacroTokens)
297 if (Tok.isAnyIdentifier())
298 rememberExpressionName(Tok);
299}
300
301void MacroToEnumCallbacks::FileChanged(SourceLocation Loc,
302 FileChangeReason Reason,
303 SrcMgr::CharacteristicKind FileType,
304 FileID PrevFID) {
305 newEnum();
306 if (Reason == EnterFile) {
307 Files.emplace_back();
308 if (!SM.isInMainFile(Loc))
309 Files.back().GuardScanner = IncludeGuard::FileChanged;
310 } else if (Reason == ExitFile) {
311 assert(CurrentFile->ConditionScopes == 0);
312 Files.pop_back();
313 }
314 CurrentFile = &Files.back();
315}
316
317bool MacroToEnumCallbacks::isInitializer(ArrayRef<Token> MacroTokens) {
318 IntegralLiteralExpressionMatcher Matcher(MacroTokens, LangOpts.C99 == 0);
319 const bool Matched = Matcher.match();
320 const bool IsC = !LangOpts.CPlusPlus;
321 if (IsC && (Matcher.largestLiteralSize() != LiteralSize::Int &&
322 Matcher.largestLiteralSize() != LiteralSize::UnsignedInt))
323 return false;
324
325 return Matched;
326}
327
328// Any defined but rejected macro is scanned for identifiers that
329// are to be excluded as enums.
330void MacroToEnumCallbacks::MacroDefined(const Token &MacroNameTok,
331 const MacroDirective *MD) {
332 // Include guards are never candidates for becoming an enum.
333 if (CurrentFile->GuardScanner == IncludeGuard::IfGuard) {
334 CurrentFile->GuardScanner = IncludeGuard::DefineGuard;
335 return;
336 }
337
338 if (insideConditional())
339 return;
340
341 if (SM.getFilename(MD->getLocation()).empty())
342 return;
343
344 const MacroInfo *Info = MD->getMacroInfo();
345 const ArrayRef<Token> MacroTokens = Info->tokens();
346 if (Info->isBuiltinMacro() || MacroTokens.empty())
347 return;
348 if (Info->isFunctionLike()) {
349 rememberExpressionTokens(MacroTokens);
350 return;
351 }
352
353 if (!isInitializer(MacroTokens))
354 return;
355
356 if (!isConsecutiveMacro(MD))
357 newEnum();
358 Enums.back().emplace_back(MacroNameTok, MD);
359 rememberLastMacroLocation(MD);
360}
361
362// Any macro that is undefined removes all adjacent macros from consideration as
363// an enum and starts a new enum scan.
364void MacroToEnumCallbacks::MacroUndefined(const Token &MacroNameTok,
365 const MacroDefinition &MD,
366 const MacroDirective *Undef) {
367 rememberExpressionName(MacroNameTok);
368
369 auto MatchesToken = [&MacroNameTok](const EnumMacro &Macro) {
370 return getTokenName(Macro.Name) == getTokenName(MacroNameTok);
371 };
372
373 auto *It = llvm::find_if(Enums, [MatchesToken](const MacroList &MacroList) {
374 return llvm::any_of(MacroList, MatchesToken);
375 });
376 if (It != Enums.end())
377 Enums.erase(It);
378
379 clearLastMacroLocation();
380 CurrentFile->GuardScanner = IncludeGuard::None;
381}
382
383void MacroToEnumCallbacks::Endif(SourceLocation Loc, SourceLocation IfLoc) {
384 // The if directive for the include guard isn't counted in the
385 // ConditionScopes.
386 if (CurrentFile->ConditionScopes == 0 &&
387 CurrentFile->GuardScanner == IncludeGuard::DefineGuard)
388 return;
389
390 // We don't need to clear the current enum because the start of the
391 // conditional block already took care of that.
392 assert(CurrentFile->ConditionScopes > 0);
393 --CurrentFile->ConditionScopes;
394}
395
396template <size_t N>
397static bool textEquals(const char (&Needle)[N], const char *HayStack) {
398 return StringRef{HayStack, N - 1} == Needle;
399}
400
401template <size_t N> static size_t len(const char (&)[N]) { return N - 1; }
402
404 PragmaIntroducerKind Introducer) {
405 if (CurrentFile->GuardScanner != IncludeGuard::FileChanged)
406 return;
407
408 bool Invalid = false;
409 const char *Text = SM.getCharacterData(
410 Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts), &Invalid);
411 if (Invalid)
412 return;
413
414 while (*Text && std::isspace(*Text))
415 ++Text;
416
417 if (textEquals("pragma", Text))
418 return;
419
420 Text += len("pragma");
421 while (*Text && std::isspace(*Text))
422 ++Text;
423
424 if (textEquals("once", Text))
425 CurrentFile->GuardScanner = IncludeGuard::IfGuard;
426}
427
428void MacroToEnumCallbacks::invalidateExpressionNames() {
429 for (const std::string &Id : ExpressionNames) {
430 llvm::erase_if(Enums, [Id](const MacroList &MacroList) {
431 return llvm::any_of(MacroList, [&Id](const EnumMacro &Macro) {
432 return getTokenName(Macro.Name) == Id;
433 });
434 });
435 }
436}
437
439 invalidateExpressionNames();
440 issueDiagnostics();
441}
442
444 llvm::erase_if(Enums, [Range](const MacroList &MacroList) {
445 return llvm::any_of(MacroList, [Range](const EnumMacro &Macro) {
446 return Macro.Directive->getLocation() >= Range.getBegin() &&
447 Macro.Directive->getLocation() <= Range.getEnd();
448 });
449 });
450}
451
452void MacroToEnumCallbacks::issueDiagnostics() {
453 for (const MacroList &MacroList : Enums) {
454 if (MacroList.empty())
455 continue;
456
457 for (const EnumMacro &Macro : MacroList)
458 warnMacroEnum(Macro);
459
460 fixEnumMacro(MacroList);
461 }
462}
463
464void MacroToEnumCallbacks::warnMacroEnum(const EnumMacro &Macro) const {
465 Check->diag(Macro.Directive->getLocation(),
466 "macro '%0' defines an integral constant; prefer an enum instead")
467 << getTokenName(Macro.Name);
468}
469
470void MacroToEnumCallbacks::fixEnumMacro(const MacroList &MacroList) const {
471 SourceLocation Begin =
472 MacroList.front().Directive->getMacroInfo()->getDefinitionLoc();
473 Begin = SM.translateLineCol(SM.getFileID(Begin),
474 SM.getSpellingLineNumber(Begin), 1);
475 const DiagnosticBuilder Diagnostic =
476 Check->diag(Begin, "replace macro with enum")
477 << FixItHint::CreateInsertion(Begin, "enum {\n");
478
479 for (size_t I = 0U; I < MacroList.size(); ++I) {
480 const EnumMacro &Macro = MacroList[I];
481 const SourceLocation DefineEnd =
482 Macro.Directive->getMacroInfo()->getDefinitionLoc();
483 const SourceLocation DefineBegin = SM.translateLineCol(
484 SM.getFileID(DefineEnd), SM.getSpellingLineNumber(DefineEnd), 1);
485 CharSourceRange DefineRange;
486 DefineRange.setBegin(DefineBegin);
487 DefineRange.setEnd(DefineEnd);
488 Diagnostic << FixItHint::CreateRemoval(DefineRange);
489
490 const SourceLocation NameEnd = Lexer::getLocForEndOfToken(
491 Macro.Directive->getMacroInfo()->getDefinitionLoc(), 0, SM, LangOpts);
492 Diagnostic << FixItHint::CreateInsertion(NameEnd, " =");
493
494 const SourceLocation ValueEnd = Lexer::getLocForEndOfToken(
495 Macro.Directive->getMacroInfo()->getDefinitionEndLoc(), 0, SM,
496 LangOpts);
497 if (I < MacroList.size() - 1)
498 Diagnostic << FixItHint::CreateInsertion(ValueEnd, ",");
499 }
500
501 SourceLocation End = Lexer::getLocForEndOfToken(
502 MacroList.back().Directive->getMacroInfo()->getDefinitionEndLoc(), 0, SM,
503 LangOpts);
504 End = SM.translateLineCol(SM.getFileID(End),
505 SM.getSpellingLineNumber(End) + 1, 1);
506 Diagnostic << FixItHint::CreateInsertion(End, "};\n");
507}
508
509void MacroToEnumCheck::registerPPCallbacks(const SourceManager &SM,
510 Preprocessor *PP,
511 Preprocessor *ModuleExpanderPP) {
512 auto Callback =
513 std::make_unique<MacroToEnumCallbacks>(this, getLangOpts(), SM);
514 PPCallback = Callback.get();
515 PP->addPPCallbacks(std::move(Callback));
516}
517
518void MacroToEnumCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
519 using namespace ast_matchers;
520 auto TopLevelDecl = hasParent(translationUnitDecl());
521 Finder->addMatcher(decl(TopLevelDecl).bind("top"), this);
522}
523
524static bool isValid(SourceRange Range) {
525 return Range.getBegin().isValid() && Range.getEnd().isValid();
526}
527
528static bool empty(SourceRange Range) {
529 return Range.getBegin() == Range.getEnd();
530}
531
533 const ast_matchers::MatchFinder::MatchResult &Result) {
534 auto *TLDecl = Result.Nodes.getNodeAs<Decl>("top");
535 if (TLDecl == nullptr)
536 return;
537
538 SourceRange Range = TLDecl->getSourceRange();
539 if (auto *TemplateFn = Result.Nodes.getNodeAs<FunctionTemplateDecl>("top")) {
540 if (TemplateFn->isThisDeclarationADefinition() && TemplateFn->hasBody())
541 Range = SourceRange{TemplateFn->getBeginLoc(),
542 TemplateFn->getUnderlyingDecl()->getBodyRBrace()};
543 }
544
545 if (isValid(Range) && !empty(Range))
546 PPCallback->invalidateRange(Range);
547}
548
549} // namespace clang::tidy::modernize
void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) override
void PragmaDirective(SourceLocation Loc, PragmaIntroducerKind Introducer) override
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule, bool ModuleImported, SrcMgr::CharacteristicKind FileType) override
MacroToEnumCallbacks(MacroToEnumCheck *Check, const LangOptions &LangOptions, const SourceManager &SM)
void Elifdef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
void If(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue) override
void Elif(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue, SourceLocation IfLoc) override
void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, const MacroDirective *Undef) override
void Ifndef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
void FileChanged(SourceLocation Loc, FileChangeReason Reason, SrcMgr::CharacteristicKind FileType, FileID PrevFID) override
void Elifndef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
void Endif(SourceLocation Loc, SourceLocation IfLoc) override
void Elifdef(SourceLocation Loc, SourceRange ConditionRange, SourceLocation IfLoc) override
void Elifndef(SourceLocation Loc, SourceRange ConditionRange, SourceLocation IfLoc) override
void Ifdef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
Replaces groups of related macros with an unscoped anonymous enum.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static bool empty(SourceRange Range)
static bool hasOnlyComments(SourceLocation Loc, const LangOptions &Options, StringRef Text)
static bool isValid(SourceRange Range)
static StringRef getTokenName(const Token &Tok)
static bool textEquals(const char(&Needle)[N], const char *HayStack)
static size_t len(const char(&)[N])