| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2016, 2019, 2020 The University of Utah |
| 4 | // Copyright (c) 2012 Konstantin Tokarev <[email protected]> |
| 5 | // All rights reserved. |
| 6 | // |
| 7 | // This file is distributed under the University of Illinois Open Source |
| 8 | // License. See the file COPYING for details. |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #if HAVE_CONFIG_H |
| 13 | # include <config.h> |
| 14 | #endif |
| 15 | |
| 16 | #include "RemoveUnusedEnumMember.h" |
| 17 | |
| 18 | #include <cctype> |
| 19 | #include "clang/AST/RecursiveASTVisitor.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/Lex/Lexer.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
| 23 | |
| 24 | #include "TransformationManager.h" |
| 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | static const char *DescriptionMsg = |
| 29 | "Remove unused enum member declarations. \n"; |
| 30 | |
| 31 | static RegisterTransformation<RemoveUnusedEnumMember> |
| 32 | Trans("remove-unused-enum-member", DescriptionMsg); |
| 33 | |
| 34 | class RemoveUnusedEnumMemberAnalysisVisitor : public |
| 35 | RecursiveASTVisitor<RemoveUnusedEnumMemberAnalysisVisitor> { |
| 36 | public: |
| 37 | |
| 38 | explicit RemoveUnusedEnumMemberAnalysisVisitor( |
| 39 | RemoveUnusedEnumMember *Instance) |
| 40 | : ConsumerInstance(Instance) |
| 41 | { } |
| 42 | |
| 43 | bool VisitEnumDecl(EnumDecl *ED); |
| 44 | |
| 45 | private: |
| 46 | |
| 47 | RemoveUnusedEnumMember *ConsumerInstance; |
| 48 | }; |
| 49 | |
| 50 | bool RemoveUnusedEnumMemberAnalysisVisitor::VisitEnumDecl(EnumDecl *ED) |
| 51 | { |
| 52 | if (ConsumerInstance->isInIncludedFile(ED) || ED != ED->getCanonicalDecl()) |
| 53 | return true; |
| 54 | |
| 55 | /* Make it backward compatible where --to-counter is unset. */ |
| 56 | if (ConsumerInstance->ToCounter == -1) |
| 57 | ConsumerInstance->ToCounter = ConsumerInstance->TransformationCounter; |
| 58 | |
| 59 | for (EnumDecl::enumerator_iterator I = ED->enumerator_begin(), |
| 60 | E = ED->enumerator_end(); I != E; ++I) { |
| 61 | if (!(*I)->isReferenced()) { |
| 62 | ConsumerInstance->ValidInstanceNum++; |
| 63 | if (ConsumerInstance->ValidInstanceNum >= |
| 64 | ConsumerInstance->TransformationCounter |
| 65 | && ConsumerInstance->ValidInstanceNum <= |
| 66 | ConsumerInstance->ToCounter) |
| 67 | ConsumerInstance->EnumValues.push_back (I); |
| 68 | } |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | void RemoveUnusedEnumMember::Initialize(ASTContext &context) |
| 74 | { |
| 75 | Transformation::Initialize(context); |
| 76 | AnalysisVisitor = new RemoveUnusedEnumMemberAnalysisVisitor(this); |
| 77 | } |
| 78 | |
| 79 | void RemoveUnusedEnumMember::HandleTranslationUnit(ASTContext &Ctx) |
| 80 | { |
| 81 | AnalysisVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 82 | |
| 83 | if (QueryInstanceOnly) |
| 84 | return; |
| 85 | |
| 86 | if (TransformationCounter > ValidInstanceNum) { |
| 87 | TransError = TransMaxInstanceError; |
| 88 | return; |
| 89 | } |
| 90 | if (ToCounter != -1 && ToCounter > ValidInstanceNum) { |
| 91 | TransError = TransToCounterTooBigError; |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 96 | |
| 97 | for (auto ev : EnumValues) { |
| 98 | removeEnumConstantDecl(ev); |
| 99 | } |
| 100 | |
| 101 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 102 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 103 | TransError = TransInternalError; |
| 104 | } |
| 105 | |
| 106 | void RemoveUnusedEnumMember::removeEnumConstantDecl(clang::EnumDecl::enumerator_iterator it) |
| 107 | { |
| 108 | SourceLocation StartLoc = it->getBeginLoc(); |
| 109 | if (StartLoc.isMacroID()) { |
| 110 | CharSourceRange Range = SrcManager->getExpansionRange(StartLoc); |
| 111 | StartLoc = Range.getBegin(); |
| 112 | } |
| 113 | SourceLocation EndLoc = it->getEndLoc(); |
| 114 | if (EndLoc.isMacroID()) { |
| 115 | CharSourceRange Range = SrcManager->getExpansionRange(EndLoc); |
| 116 | EndLoc = Range.getEnd(); |
| 117 | } |
| 118 | SourceLocation CommaLoc = Lexer::findLocationAfterToken( |
| 119 | EndLoc, tok::comma, *SrcManager, Context->getLangOpts(), |
| 120 | /*SkipTrailingWhitespaceAndNewLine=*/false); |
| 121 | if (CommaLoc.isValid()) |
| 122 | EndLoc = CommaLoc; |
| 123 | TheRewriter.RemoveText(SourceRange(StartLoc, EndLoc)); |
| 124 | } |
| 125 | |
| 126 | RemoveUnusedEnumMember::~RemoveUnusedEnumMember() |
| 127 | { |
| 128 | delete AnalysisVisitor; |
| 129 | } |
| 130 |