summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-modernize/Core/FileOverrides.cpp
blob: 7ab7e91c30c083bd2909cb74903e602a447880cb (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//===-- Core/FileOverrides.cpp --------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file provides types and functionality for dealing with source
/// and header file content overrides.
///
//===----------------------------------------------------------------------===//

#include "Core/FileOverrides.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Tooling/ReplacementsYaml.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
#include <algorithm>

using namespace clang;
using namespace clang::tooling;

bool generateReplacementsFileName(const llvm::StringRef MainSourceFile,
                                  llvm::SmallVectorImpl<char> &Result,
                                  llvm::SmallVectorImpl<char> &Error) {
  using namespace llvm::sys;

  Error.clear();
  if (llvm::error_code EC = fs::createUniqueFile(
          MainSourceFile + "_%%_%%_%%_%%_%%_%%.yaml", Result)) {
    Error.append(EC.message().begin(), EC.message().end());
    return false;
  }

  return true;
}

namespace {

/// \brief Comparator to be able to order tooling::Range based on their offset.
bool rangeLess(clang::tooling::Range A, clang::tooling::Range B) {
  if (A.getOffset() == B.getOffset())
    return A.getLength() < B.getLength();
  return A.getOffset() < B.getOffset();
}

/// \brief Functor that returns the given range without its overlaps with the
/// replacement given in the constructor.
struct RangeReplacedAdjuster {
  RangeReplacedAdjuster(const tooling::Replacement &Replace)
      : Replace(Replace.getOffset(), Replace.getLength()),
        ReplaceNewSize(Replace.getReplacementText().size()) {}

  tooling::Range operator()(clang::tooling::Range Range) const {
    if (!Range.overlapsWith(Replace))
      return Range;
    // range inside replacement -> make the range length null
    if (Replace.contains(Range))
      return tooling::Range(Range.getOffset(), 0);
    // replacement inside range -> resize the range
    if (Range.contains(Replace)) {
      int Difference = ReplaceNewSize - Replace.getLength();
      return tooling::Range(Range.getOffset(), Range.getLength() + Difference);
    }
    // beginning of the range replaced -> truncate range beginning
    if (Range.getOffset() > Replace.getOffset()) {
      unsigned ReplaceEnd = Replace.getOffset() + Replace.getLength();
      unsigned RangeEnd = Range.getOffset() + Range.getLength();
      return tooling::Range(ReplaceEnd, RangeEnd - ReplaceEnd);
    }
    // end of the range replaced -> truncate range end
    if (Range.getOffset() < Replace.getOffset())
      return tooling::Range(Range.getOffset(),
                            Replace.getOffset() - Range.getOffset());
    llvm_unreachable("conditions not handled properly");
  }

  const tooling::Range Replace;
  const unsigned ReplaceNewSize;
};

} // end anonymous namespace

void
ChangedRanges::adjustChangedRanges(const tooling::ReplacementsVec &Replaces) {
  // first adjust existing ranges in case they overlap with the replacements
  for (ReplacementsVec::const_iterator I = Replaces.begin(), E = Replaces.end();
       I != E; ++I) {
    const tooling::Replacement &Replace = *I;

    std::transform(Ranges.begin(), Ranges.end(), Ranges.begin(),
                   RangeReplacedAdjuster(Replace));
  }

  // then shift existing ranges to reflect the new positions
  for (RangeVec::iterator I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
    unsigned ShiftedOffset =
        tooling::shiftedCodePosition(Replaces, I->getOffset());
    *I = tooling::Range(ShiftedOffset, I->getLength());
  }

  // then generate the new ranges from the replacements
  for (ReplacementsVec::const_iterator I = Replaces.begin(), E = Replaces.end();
       I != E; ++I) {
    const tooling::Replacement &R = *I;
    unsigned Offset = tooling::shiftedCodePosition(Replaces, R.getOffset());
    unsigned Length = R.getReplacementText().size();

    Ranges.push_back(tooling::Range(Offset, Length));
  }

  // cleanups unecessary ranges to finish
  coalesceRanges();
}

void ChangedRanges::coalesceRanges() {
  // sort the ranges by offset and then for each group of adjacent/overlapping
  // ranges the first one in the group is extended to cover the whole group.
  std::sort(Ranges.begin(), Ranges.end(), &rangeLess);
  RangeVec::iterator FirstInGroup = Ranges.begin();
  assert(!Ranges.empty() && "unexpected empty vector");
  for (RangeVec::iterator I = Ranges.begin() + 1, E = Ranges.end(); I != E;
       ++I) {
    unsigned GroupEnd = FirstInGroup->getOffset() + FirstInGroup->getLength();

    // no contact
    if (I->getOffset() > GroupEnd)
      FirstInGroup = I;
    else {
      unsigned GrpBegin = FirstInGroup->getOffset();
      unsigned GrpEnd = std::max(GroupEnd, I->getOffset() + I->getLength());
      *FirstInGroup = tooling::Range(GrpBegin, GrpEnd - GrpBegin);
    }
  }

  // remove the ranges that are covered by the first member of the group
  Ranges.erase(std::unique(Ranges.begin(), Ranges.end(),
                           std::mem_fun_ref(&Range::contains)),
               Ranges.end());
}

void FileOverrides::applyOverrides(clang::SourceManager &SM) const {
  FileManager &FM = SM.getFileManager();

  for (FileStateMap::const_iterator I = FileStates.begin(),
                                    E = FileStates.end();
       I != E; ++I) {
    SM.overrideFileContents(FM.getFile(I->getKey()),
                            llvm::MemoryBuffer::getMemBuffer(I->getValue()));
  }
}

void FileOverrides::adjustChangedRanges(
    const clang::replace::FileToReplacementsMap &Replaces) {

  for (replace::FileToReplacementsMap::const_iterator I = Replaces.begin(),
       E = Replaces.end(); I != E; ++I) {
    ChangeTracking[I->getKey()].adjustChangedRanges(I->getValue());
  }
}

void FileOverrides::updateState(const clang::Rewriter &Rewrites) {
  for (Rewriter::const_buffer_iterator BufferI = Rewrites.buffer_begin(),
                                       BufferE = Rewrites.buffer_end();
       BufferI != BufferE; ++BufferI) {
    const char *FileName =
        Rewrites.getSourceMgr().getFileEntryForID(BufferI->first)->getName();
    const RewriteBuffer &RewriteBuf = BufferI->second;
    FileStates[FileName].assign(RewriteBuf.begin(), RewriteBuf.end());
  }
}

bool FileOverrides::writeToDisk(DiagnosticsEngine &Diagnostics) const {
  bool Errors = false;
  for (FileStateMap::const_iterator I = FileStates.begin(),
                                    E = FileStates.end();
       I != E; ++I) {
    std::string ErrorInfo;
    // The extra transform through std::string is to ensure null-termination
    // of the filename stored as the key of the StringMap.
    llvm::raw_fd_ostream FileStream(I->getKey().str().c_str(), ErrorInfo);
    if (!ErrorInfo.empty()) {
      llvm::errs() << "Failed to write new state for " << I->getKey() << ".\n";
      Errors = true;
    }
    FileStream << I->getValue();
  }
  return !Errors;
}
OpenPOWER on IntegriCloud