2013-08-29 21:42:13 +08:00
|
|
|
//===-- PassByValue.h -------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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 declaration of the PassByValueTransform
|
|
|
|
/// class.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-09-05 03:13:50 +08:00
|
|
|
#ifndef CLANG_MODERNIZE_PASS_BY_VALUE_H
|
|
|
|
#define CLANG_MODERNIZE_PASS_BY_VALUE_H
|
2013-08-29 21:42:13 +08:00
|
|
|
|
|
|
|
#include "Core/IncludeDirectives.h"
|
2014-01-08 04:05:01 +08:00
|
|
|
#include "Core/Transform.h"
|
2013-08-29 21:42:13 +08:00
|
|
|
|
|
|
|
class ConstructorParamReplacer;
|
|
|
|
|
|
|
|
/// \brief Subclass of Transform that uses pass-by-value semantic when move
|
|
|
|
/// constructors are available to avoid copies.
|
|
|
|
///
|
|
|
|
/// When a class constructor accepts an object by const reference with the
|
|
|
|
/// intention of copying the object the copy can be avoided in certain
|
|
|
|
/// situations if the object has a move constructor. First, the constructor is
|
|
|
|
/// changed to accept the object by value instead. Then this argument is moved
|
|
|
|
/// instead of copied into class-local storage. If an l-value is provided to the
|
|
|
|
/// constructor, there is no difference in the number of copies made. However,
|
|
|
|
/// if an r-value is passed, the copy is avoided completely.
|
|
|
|
///
|
|
|
|
/// For example, given:
|
|
|
|
/// \code
|
|
|
|
/// #include <string>
|
|
|
|
///
|
|
|
|
/// class A {
|
|
|
|
/// std::string S;
|
|
|
|
/// public:
|
|
|
|
/// A(const std::string &S) : S(S) {}
|
|
|
|
/// };
|
|
|
|
/// \endcode
|
|
|
|
/// the code is transformed to:
|
|
|
|
/// \code
|
|
|
|
/// #include <string>
|
|
|
|
///
|
|
|
|
/// class A {
|
|
|
|
/// std::string S;
|
|
|
|
/// public:
|
|
|
|
/// A(std::string S) : S(std::move(S)) {}
|
|
|
|
/// };
|
|
|
|
/// \endcode
|
|
|
|
class PassByValueTransform : public Transform {
|
|
|
|
public:
|
|
|
|
PassByValueTransform(const TransformOptions &Options)
|
2014-06-09 10:03:06 +08:00
|
|
|
: Transform("PassByValue", Options), Replacer(nullptr) {}
|
2013-08-29 21:42:13 +08:00
|
|
|
|
|
|
|
/// \see Transform::apply().
|
2013-10-05 20:15:58 +08:00
|
|
|
virtual int apply(const clang::tooling::CompilationDatabase &Database,
|
2014-03-02 18:20:11 +08:00
|
|
|
const std::vector<std::string> &SourcePaths) override;
|
2013-08-29 21:42:13 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief Setups the \c IncludeDirectives for the replacer.
|
|
|
|
virtual bool handleBeginSource(clang::CompilerInstance &CI,
|
2014-03-02 18:20:11 +08:00
|
|
|
llvm::StringRef Filename) override;
|
2013-08-29 21:42:13 +08:00
|
|
|
|
2014-03-09 17:24:40 +08:00
|
|
|
std::unique_ptr<IncludeDirectives> IncludeManager;
|
2013-08-29 21:42:13 +08:00
|
|
|
ConstructorParamReplacer *Replacer;
|
|
|
|
};
|
|
|
|
|
2013-09-05 03:13:50 +08:00
|
|
|
#endif // CLANG_MODERNIZE_PASS_BY_VALUE_H
|