MacroToEnumCheck.cpp 18 KB

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