File: ReplaceArrayAccessWithIndex.cpp

package info (click to toggle)
creduce 2.9~20181016-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 4,360 kB
  • sloc: cpp: 23,977; ansic: 7,062; sh: 4,918; perl: 2,679; makefile: 472; lex: 441
file content (145 lines) | stat: -rw-r--r-- 3,874 bytes parent folder | download | duplicates (9)
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
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2016, 2017 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 "ReplaceArrayAccessWithIndex.h"

#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/SourceManager.h"

#include "TransformationManager.h"

#include <iostream>

using namespace clang;


static const char *Description =
  "Replace array accesses with the index expression.";

static RegisterTransformation<ReplaceArrayAccessWithIndex>
Trans("replace-array-access-with-index", Description);

class ReplaceArrayAccessWithIndex::IndexCollector
  : public RecursiveASTVisitor<ReplaceArrayAccessWithIndex::IndexCollector>
{
public:
  explicit IndexCollector(ReplaceArrayAccessWithIndex *instance);
  bool VisitArraySubscriptExpr(ArraySubscriptExpr *ASE);

private:
  const VarDecl *getVarDeclFromExpr(const Expr *E);
  ReplaceArrayAccessWithIndex *ConsumerInstance;
};

ReplaceArrayAccessWithIndex::IndexCollector::IndexCollector(
  ReplaceArrayAccessWithIndex *instance)
  : ConsumerInstance(instance)
{
  // No further initialization needed.
}

bool ReplaceArrayAccessWithIndex::IndexCollector::VisitArraySubscriptExpr(
  ArraySubscriptExpr *ASE)
{
  // Skip expressions in included files.
  if (ConsumerInstance->isInIncludedFile(ASE))
    return true;

  const VarDecl *BaseVD = getVarDeclFromExpr(ASE->getBase());

  if (!BaseVD)
    return true;

  ArrayType const *ArrayTy = dyn_cast<ArrayType>(BaseVD->getType().getTypePtr());
  // Only apply the transformation to one-dimensional arrays of scalars.
  if (!ArrayTy || !ArrayTy->getElementType().getTypePtr()->isScalarType())
    return true;

  ConsumerInstance->ASEs.push_back(ASE);
  ConsumerInstance->ValidInstanceNum++;

  return true;
}

const VarDecl *ReplaceArrayAccessWithIndex::IndexCollector::getVarDeclFromExpr(
  const Expr *E)
{
  TransAssert(E && "NULL Expr!");
  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts());
  if (!DRE)
    return NULL;
  const ValueDecl *OrigDecl = DRE->getDecl();
  const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl);
  if (!VD)
    return NULL;
  const VarDecl *CanonicalVD = VD->getCanonicalDecl();
  return CanonicalVD;
}



ReplaceArrayAccessWithIndex::~ReplaceArrayAccessWithIndex(void)
{
  delete Collector;
}


void ReplaceArrayAccessWithIndex::Initialize(clang::ASTContext &context)
{
  Transformation::Initialize(context);
  Collector = new IndexCollector(this);
}

void ReplaceArrayAccessWithIndex::HandleTranslationUnit(clang::ASTContext &Ctx)
{
  TransAssert(Collector && "NULL Collector");
  Collector->TraverseDecl(Ctx.getTranslationUnitDecl());

  if (QueryInstanceOnly)
    return;

  if (TransformationCounter > ValidInstanceNum) {
    TransError = TransMaxInstanceError;
    return;
  }

  Ctx.getDiagnostics().setSuppressAllDiagnostics(false);
  doRewrite();

  if (Ctx.getDiagnostics().hasErrorOccurred() ||
      Ctx.getDiagnostics().hasFatalErrorOccurred())
    TransError = TransInternalError;
}

void ReplaceArrayAccessWithIndex::doRewrite(void)
{
  ArraySubscriptExpr const *ASE = ASEs[TransformationCounter - 1];
  Expr const *Idx = ASE->getIdx();

  TransAssert(Idx && "Bad Idx!");

  std::string IdxStr;
  RewriteHelper->getExprString(Idx, IdxStr);

  QualType ASEType = ASE->getType().getCanonicalType();
  QualType IdxType = Idx->getType().getCanonicalType();

  if (ASEType != IdxType) {
    IdxStr = std::string("(") + ASEType.getAsString() + std::string(")")+
      std::string("(") + IdxStr + std::string(")");
  }

  RewriteHelper->replaceExpr(ASE, IdxStr);
}