Skip to content

Commit 2f6888e

Browse files
Merge pull request #433 from andreasfertig/missingUtilityHeader
Added insertion of utility header if C++ Insights add a `std::move`.
2 parents 6a4150e + 4812dfb commit 2f6888e

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed

CodeGenerator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,6 +3419,7 @@ void CodeGenerator::HandleLocalStaticNonTrivialClass(const VarDecl* stmt)
34193419

34203420
// Tests show that the compiler does better than std::move
34213421
mOutputFormatHelper.Append(typeName, "(std::move("sv);
3422+
mHaveMovedLambda = true;
34223423
}
34233424

34243425
InsertArg(init);

CodeGenerator.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ class CodeGenerator
177177
/// Track whether we have a noexcept transformation which needs the exception header.
178178
static bool NeedToInsertExceptionHeader() { return mHaveException; }
179179

180+
/// Track whether we inserted a std::move due, to a static transformation, this means we need the utility header.
181+
static bool NeedToInsertUtilityHeader() { return mHaveMovedLambda; }
182+
180183
template<typename T>
181184
void InsertTemplateArgs(const ArrayRef<T>& array)
182185
{
@@ -352,7 +355,8 @@ class CodeGenerator
352355
NoEmptyInitList mNoEmptyInitList{
353356
NoEmptyInitList::No}; //!< At least in case if a requires-clause containing T{} we don't want to get T{{}}.
354357
const LambdaExpr* mLambdaExpr{};
355-
static inline bool mHaveLocalStatic; //!< Track whether there was a thread-safe \c static in the code. This
358+
static inline bool mHaveLocalStatic; //!< Track whether there was a thread-safe \c static in the code.
359+
static inline bool mHaveMovedLambda; //!< Track whether there was a std::move inserted.
356360
static inline bool
357361
mHaveException; //!< Track whether there was a noexcept transformation requireing the exception header.
358362
static constexpr auto MAX_FILL_VALUES_FOR_ARRAYS{

Insights.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ class CppInsightASTConsumer final : public ASTConsumer
111111

112112
// Check whether we had static local variables which we transformed. Then for the placement-new we need to
113113
// include the header <new>.
114-
if(CodeGenerator::NeedToInsertNewHeader() || CodeGenerator::NeedToInsertExceptionHeader()) {
114+
if(CodeGenerator::NeedToInsertNewHeader() || CodeGenerator::NeedToInsertExceptionHeader() ||
115+
CodeGenerator::NeedToInsertUtilityHeader()) {
115116
const auto& sm = context.getSourceManager();
116117
const auto& mainFileId = sm.getMainFileID();
117118
const auto loc = sm.translateFileLineCol(sm.getFileEntryForID(mainFileId), 1, 1);
@@ -121,9 +122,15 @@ class CppInsightASTConsumer final : public ASTConsumer
121122
loc,
122123
"#include <new> // for thread-safe static's placement new\n#include <stdint.h> // for "
123124
"uint64_t under Linux/GCC\n"sv);
124-
} else {
125+
}
126+
127+
if(CodeGenerator::NeedToInsertExceptionHeader()) {
125128
mRewriter.InsertText(loc, "#include <exception> // for noexcept transformation\n"sv);
126129
}
130+
131+
if(CodeGenerator::NeedToInsertUtilityHeader()) {
132+
mRewriter.InsertText(loc, "#include <utility> // std::move\n"sv);
133+
}
127134
}
128135
}
129136

tests/Issue369_1.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <new> // for thread-safe static's placement new
22
#include <stdint.h> // for uint64_t under Linux/GCC
3+
#include <utility> // std::move
34
struct foo
45
{
56
foo();

tests/Issue369_2.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <new> // for thread-safe static's placement new
22
#include <stdint.h> // for uint64_t under Linux/GCC
3+
#include <utility> // std::move
34
struct foo
45
{
56
foo();

0 commit comments

Comments
 (0)