| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2019 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 "RemoveEnumMemberValue.h" |
| 17 | |
| 18 | #include <cctype> |
| 19 | #include "clang/AST/RecursiveASTVisitor.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | |
| 23 | #include "TransformationManager.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | static const char *DescriptionMsg = |
| 28 | "Remove enum member value. \n"; |
| 29 | |
| 30 | static RegisterTransformation<RemoveEnumMemberValue> |
| 31 | Trans("remove-enum-member-value", DescriptionMsg); |
| 32 | |
| 33 | class RemoveEnumMemberValueAnalysisVisitor : public |
| 34 | RecursiveASTVisitor<RemoveEnumMemberValueAnalysisVisitor> { |
| 35 | public: |
| 36 | |
| 37 | explicit RemoveEnumMemberValueAnalysisVisitor(RemoveEnumMemberValue *Instance) |
| 38 | : ConsumerInstance(Instance) |
| 39 | { } |
| 40 | |
| 41 | bool VisitEnumConstantDecl(EnumConstantDecl *ECD); |
| 42 | |
| 43 | private: |
| 44 | |
| 45 | RemoveEnumMemberValue *ConsumerInstance; |
| 46 | }; |
| 47 | |
| 48 | bool RemoveEnumMemberValueAnalysisVisitor::VisitEnumConstantDecl( |
| 49 | EnumConstantDecl *ECD) |
| 50 | { |
| 51 | if (ConsumerInstance->isInIncludedFile(ECD) || !ECD->getInitExpr()) |
| 52 | return true; |
| 53 | |
| 54 | ConsumerInstance->ValidInstanceNum++; |
| 55 | if (ConsumerInstance->ValidInstanceNum == |
| 56 | ConsumerInstance->TransformationCounter) { |
| 57 | ConsumerInstance->TheEnumConstantDecl = ECD; |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | void RemoveEnumMemberValue::Initialize(ASTContext &context) |
| 63 | { |
| 64 | Transformation::Initialize(context); |
| 65 | AnalysisVisitor = new RemoveEnumMemberValueAnalysisVisitor(this); |
| 66 | } |
| 67 | |
| 68 | void RemoveEnumMemberValue::HandleTranslationUnit(ASTContext &Ctx) |
| 69 | { |
| 70 | AnalysisVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 71 | |
| 72 | if (QueryInstanceOnly) |
| 73 | return; |
| 74 | |
| 75 | if (TransformationCounter > ValidInstanceNum) { |
| 76 | TransError = TransMaxInstanceError; |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 81 | |
| 82 | TransAssert(TheEnumConstantDecl && "NULL TheEnumConstantDecl!"); |
| 83 | |
| 84 | removeEnumValue(); |
| 85 | |
| 86 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 87 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 88 | TransError = TransInternalError; |
| 89 | } |
| 90 | |
| 91 | static int getOffset(const char *Buf, char Symbol) |
| 92 | { |
| 93 | int Offset = 0; |
| 94 | while (*Buf != Symbol) { |
| 95 | Buf--; |
| 96 | if (*Buf == '\0') |
| 97 | break; |
| 98 | Offset--; |
| 99 | } |
| 100 | return Offset; |
| 101 | } |
| 102 | |
| 103 | void RemoveEnumMemberValue::removeEnumValue() |
| 104 | { |
| 105 | SourceManager &SrcManager = TheRewriter.getSourceMgr(); |
| 106 | SourceRange Range = TheEnumConstantDecl->getInitExpr()->getSourceRange(); |
| 107 | SourceLocation StartLoc = Range.getBegin(); |
| 108 | if (StartLoc.isMacroID()) { |
| 109 | Range = SrcManager.getExpansionRange(StartLoc).getAsRange(); |
| 110 | StartLoc = SrcManager.getExpansionLoc(StartLoc); |
| 111 | } |
| 112 | const char *Buf = SrcManager.getCharacterData(StartLoc); |
| 113 | int offset = getOffset(Buf, '='); |
| 114 | Range.setBegin(StartLoc.getLocWithOffset(offset)); |
| 115 | TheRewriter.RemoveText(Range); |
| 116 | } |
| 117 | |
| 118 | RemoveEnumMemberValue::~RemoveEnumMemberValue() |
| 119 | { |
| 120 | delete AnalysisVisitor; |
| 121 | } |
| 122 |