1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2012, 2015, 2016 The University of Utah
// All rights reserved.
//
// This file is distributed under the University of Illinois Open Source
// License. See the file COPYING for details.
//
//===----------------------------------------------------------------------===//
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "MoveGlobalVar.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/SourceManager.h"
#include "TransformationManager.h"
using namespace clang;
static const char *DescriptionMsg =
"Try to move global var/struct/union declarations above all function \
declarations except printf. Also move the declaration of printf to the top of \
the reduce code if it exists and is not at the top of the code.\n";
static RegisterTransformation<MoveGlobalVar>
Trans("move-global-var", DescriptionMsg);
void MoveGlobalVar::Initialize(ASTContext &context)
{
Transformation::Initialize(context);
}
bool MoveGlobalVar::isSpecialDecl(const std::string &Name)
{
return ((Name == "__va_list_tag") ||
(Name == "__builtin_va_list"));
}
bool MoveGlobalVar::HandleTopLevelDecl(DeclGroupRef D)
{
if (TransformationManager::isCXXLangOpt()) {
ValidInstanceNum = 0;
return true;
}
DeclGroupRef::iterator I = D.begin();
TransAssert((I != D.end()) && "Bad DeclGroupRef!");
if (isInIncludedFile(*I))
return true;
const NamedDecl *ND = dyn_cast<NamedDecl>(*I);
if (!TheFirstDecl && ND && isSpecialDecl(ND->getNameAsString()))
return true;
FunctionDecl *FD = dyn_cast<FunctionDecl>(*I);
if (FD) {
handleFunctionDecl(FD);
}
else {
handleOtherDecl(D);
}
if (!TheFirstDecl)
TheFirstDecl = (*I);
return true;
}
void MoveGlobalVar::HandleTranslationUnit(ASTContext &Ctx)
{
if (QueryInstanceOnly)
return;
if (TransformationCounter > ValidInstanceNum) {
TransError = TransMaxInstanceError;
return;
}
Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
if (ThePrintfDecl)
liftPrintfDecl();
else
liftOtherDecl();
if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
TransError = TransInternalError;
}
void MoveGlobalVar::handleFunctionDecl(const FunctionDecl *FD)
{
if (FD->getNameAsString() == "printf") {
if (ThePrintfDecl || !TheFirstDecl || FD->isThisDeclarationADefinition())
return;
ValidInstanceNum++;
if (TransformationCounter == ValidInstanceNum)
ThePrintfDecl = FD;
}
else if (!TheFirstFunctionDecl) {
TheFirstFunctionDecl = FD;
}
}
void MoveGlobalVar::handleOtherDecl(DeclGroupRef DGR)
{
if (!TheFirstFunctionDecl)
return;
ValidInstanceNum++;
if (TransformationCounter == ValidInstanceNum)
TheDGRPointer = DGR.getAsOpaquePtr();
}
void MoveGlobalVar::liftPrintfDecl(void)
{
TransAssert(ThePrintfDecl && TheFirstDecl &&
(ThePrintfDecl != TheFirstDecl) && "Invalid printf decl!");
std::string PrintfDeclStr;
RewriteHelper->getFunctionDeclStrAndRemove(ThePrintfDecl, PrintfDeclStr);
SourceRange DeclRange = TheFirstDecl->getSourceRange();
SourceLocation StartLoc = DeclRange.getBegin();
PrintfDeclStr += ";\n";
TheRewriter.InsertTextBefore(StartLoc, PrintfDeclStr);
}
void MoveGlobalVar::liftOtherDecl(void)
{
TransAssert(TheDGRPointer && "NULL DGR pointer!");
TransAssert(TheFirstFunctionDecl && "NULL First Decl!");
DeclGroupRef DGR = DeclGroupRef::getFromOpaquePtr(TheDGRPointer);
std::string DGRStr;
RewriteHelper->getEntireDeclGroupStrAndRemove(DGR, DGRStr);
SourceRange DeclRange = TheFirstFunctionDecl->getSourceRange();
SourceLocation StartLoc = DeclRange.getBegin();
DGRStr += ";\n";
TheRewriter.InsertTextBefore(StartLoc, DGRStr);
}
MoveGlobalVar::~MoveGlobalVar(void)
{
// Nothing to do
}
|