blob: 5b73d51b019c3fe0c952d557db151e171a80f215 [file] [log] [blame]
Angel Garcia Gomez19016712015-08-25 13:03:43 +00001//===--- ReplaceAutoPtrCheck.h - clang-tidy----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_AUTO_PTR_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_AUTO_PTR_H
12
13#include "../ClangTidy.h"
14#include "../utils/IncludeInserter.h"
15
16namespace clang {
17namespace tidy {
18namespace modernize {
19
Alexander Kornienkod96d89f2015-09-08 09:44:04 +000020/// Transforms the deprecated `std::auto_ptr` into the C++11 `std::unique_ptr`.
Angel Garcia Gomez19016712015-08-25 13:03:43 +000021///
Alexander Kornienkod96d89f2015-09-08 09:44:04 +000022/// Note that both the `std::auto_ptr` type and the transfer of ownership are
23/// transformed. `std::auto_ptr` provides two ways to transfer the ownership,
Angel Garcia Gomez19016712015-08-25 13:03:43 +000024/// the copy-constructor and the assignment operator. Unlike most classes these
25/// operations do not 'copy' the resource but they 'steal' it.
Alexander Kornienkod96d89f2015-09-08 09:44:04 +000026/// `std::unique_ptr` uses move semantics instead, which makes the intent of
Angel Garcia Gomez19016712015-08-25 13:03:43 +000027/// transferring the resource explicit. This difference between the two smart
28/// pointers requeres to wrap the copy-ctor and assign-operator with
Alexander Kornienkod96d89f2015-09-08 09:44:04 +000029/// `std::move()`.
Angel Garcia Gomez19016712015-08-25 13:03:43 +000030///
31/// For example, given:
Alexander Kornienkod96d89f2015-09-08 09:44:04 +000032///
Angel Garcia Gomez19016712015-08-25 13:03:43 +000033/// \code
34/// std::auto_ptr<int> i, j;
35/// i = j;
36/// \endcode
Alexander Kornienkod96d89f2015-09-08 09:44:04 +000037///
Angel Garcia Gomez19016712015-08-25 13:03:43 +000038/// This code is transformed to:
Alexander Kornienkod96d89f2015-09-08 09:44:04 +000039///
Angel Garcia Gomez19016712015-08-25 13:03:43 +000040/// \code
41/// std::unique_ptr<in> i, j;
42/// i = std::move(j);
43/// \endcode
44class ReplaceAutoPtrCheck : public ClangTidyCheck {
45public:
46 ReplaceAutoPtrCheck(StringRef Name, ClangTidyContext *Context);
47 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
48 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
49 void registerPPCallbacks(CompilerInstance &Compiler) override;
50 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
51
52private:
Etienne Bergeron2a4c00f2016-05-03 02:54:05 +000053 std::unique_ptr<utils::IncludeInserter> Inserter;
54 const utils::IncludeSorter::IncludeStyle IncludeStyle;
Angel Garcia Gomez19016712015-08-25 13:03:43 +000055};
56
57} // namespace modernize
58} // namespace tidy
59} // namespace clang
60
61#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_AUTO_PTR_H