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
|
//===-- Core/Reformatting.cpp - LibFormat integration ---------------------===//
//
// 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 the LibFormat integration used to reformat
/// migrated code.
///
//===----------------------------------------------------------------------===//
#include "Core/Reformatting.h"
#include "Core/FileOverrides.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
using namespace clang;
void Reformatter::reformatChanges(SourceOverrides &Overrides) {
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts(
new DiagnosticOptions());
DiagnosticsEngine Diagnostics(
llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
DiagOpts.getPtr());
FileManager Files((FileSystemOptions()));
SourceManager SM(Diagnostics, Files);
reformatChanges(Overrides, SM);
}
void Reformatter::reformatChanges(SourceOverrides &Overrides,
clang::SourceManager &SM) {
tooling::Replacements Replaces;
Overrides.applyOverrides(SM);
if (Overrides.isSourceOverriden())
Replaces = reformatSingleFile(Overrides.getMainFileName(),
Overrides.getChangedRanges(), SM);
for (HeaderOverrides::const_iterator I = Overrides.headers_begin(),
E = Overrides.headers_end();
I != E; ++I) {
const HeaderOverride &Header = I->getValue();
const tooling::Replacements &HeaderReplaces =
reformatSingleFile(Header.FileName, Header.Changes, SM);
Replaces.insert(HeaderReplaces.begin(), HeaderReplaces.end());
}
Overrides.applyReplacements(Replaces, SM);
}
tooling::Replacements Reformatter::reformatSingleFile(
llvm::StringRef FileName, const ChangedRanges &Changes, SourceManager &SM) {
const clang::FileEntry *Entry = SM.getFileManager().getFile(FileName);
assert(Entry && "expected an existing file");
FileID ID = SM.translateFile(Entry);
if (ID.isInvalid())
ID = SM.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
std::vector<CharSourceRange> ReformatRanges;
SourceLocation StartOfFile = SM.getLocForStartOfFile(ID);
for (ChangedRanges::const_iterator I = Changes.begin(), E = Changes.end();
I != E; ++I) {
SourceLocation Start = StartOfFile.getLocWithOffset(I->getOffset());
SourceLocation End = Start.getLocWithOffset(I->getLength());
ReformatRanges.push_back(CharSourceRange::getCharRange(Start, End));
}
Lexer Lex(ID, SM.getBuffer(ID), SM, getFormattingLangOpts(Style.Standard));
return format::reformat(Style, Lex, SM, ReformatRanges);
}
|