diff options
author | Edwin Vane <edwin.vane@intel.com> | 2013-07-22 20:26:29 +0000 |
---|---|---|
committer | Edwin Vane <edwin.vane@intel.com> | 2013-07-22 20:26:29 +0000 |
commit | 55b0be72d11ce678df24d8971d5553415a0edd61 (patch) | |
tree | e2428cb71cab77800de04fd621c3816df321fc4f /clang-tools-extra/cpp11-migrate/Core/Reformatting.cpp | |
parent | 7d6753738a0a84fd3c2427fda8daf028687ccaee (diff) | |
download | bcm5719-llvm-55b0be72d11ce678df24d8971d5553415a0edd61.tar.gz bcm5719-llvm-55b0be72d11ce678df24d8971d5553415a0edd61.zip |
cp11-migrate: Integration with LibFormat
Adding a feature to optionally reformat code changed by the migrator. Like
LibFormat, can choose between built-in styles (LLVM, Mozilla, Google, Chromium)
or use a YAML-format config file.
Author: Guillaume Papin <guillaume.papin@epitech.eu>
llvm-svn: 186866
Diffstat (limited to 'clang-tools-extra/cpp11-migrate/Core/Reformatting.cpp')
-rw-r--r-- | clang-tools-extra/cpp11-migrate/Core/Reformatting.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/clang-tools-extra/cpp11-migrate/Core/Reformatting.cpp b/clang-tools-extra/cpp11-migrate/Core/Reformatting.cpp new file mode 100644 index 00000000000..3fd77612bbf --- /dev/null +++ b/clang-tools-extra/cpp11-migrate/Core/Reformatting.cpp @@ -0,0 +1,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); +} |