Skip to content

Commit

Permalink
Merge pull request #433 from andreasfertig/missingUtilityHeader
Browse files Browse the repository at this point in the history
Added insertion of utility header if C++ Insights add a `std::move`.
  • Loading branch information
andreasfertig committed Feb 3, 2022
2 parents 6a4150e + 4812dfb commit 2f6888e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3419,6 +3419,7 @@ void CodeGenerator::HandleLocalStaticNonTrivialClass(const VarDecl* stmt)

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

InsertArg(init);
Expand Down
6 changes: 5 additions & 1 deletion CodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ class CodeGenerator
/// Track whether we have a noexcept transformation which needs the exception header.
static bool NeedToInsertExceptionHeader() { return mHaveException; }

/// Track whether we inserted a std::move due, to a static transformation, this means we need the utility header.
static bool NeedToInsertUtilityHeader() { return mHaveMovedLambda; }

template<typename T>
void InsertTemplateArgs(const ArrayRef<T>& array)
{
Expand Down Expand Up @@ -352,7 +355,8 @@ class CodeGenerator
NoEmptyInitList mNoEmptyInitList{
NoEmptyInitList::No}; //!< At least in case if a requires-clause containing T{} we don't want to get T{{}}.
const LambdaExpr* mLambdaExpr{};
static inline bool mHaveLocalStatic; //!< Track whether there was a thread-safe \c static in the code. This
static inline bool mHaveLocalStatic; //!< Track whether there was a thread-safe \c static in the code.
static inline bool mHaveMovedLambda; //!< Track whether there was a std::move inserted.
static inline bool
mHaveException; //!< Track whether there was a noexcept transformation requireing the exception header.
static constexpr auto MAX_FILL_VALUES_FOR_ARRAYS{
Expand Down
11 changes: 9 additions & 2 deletions Insights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ class CppInsightASTConsumer final : public ASTConsumer

// Check whether we had static local variables which we transformed. Then for the placement-new we need to
// include the header <new>.
if(CodeGenerator::NeedToInsertNewHeader() || CodeGenerator::NeedToInsertExceptionHeader()) {
if(CodeGenerator::NeedToInsertNewHeader() || CodeGenerator::NeedToInsertExceptionHeader() ||
CodeGenerator::NeedToInsertUtilityHeader()) {
const auto& sm = context.getSourceManager();
const auto& mainFileId = sm.getMainFileID();
const auto loc = sm.translateFileLineCol(sm.getFileEntryForID(mainFileId), 1, 1);
Expand All @@ -121,9 +122,15 @@ class CppInsightASTConsumer final : public ASTConsumer
loc,
"#include <new> // for thread-safe static's placement new\n#include <stdint.h> // for "
"uint64_t under Linux/GCC\n"sv);
} else {
}

if(CodeGenerator::NeedToInsertExceptionHeader()) {
mRewriter.InsertText(loc, "#include <exception> // for noexcept transformation\n"sv);
}

if(CodeGenerator::NeedToInsertUtilityHeader()) {
mRewriter.InsertText(loc, "#include <utility> // std::move\n"sv);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/Issue369_1.expect
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <new> // for thread-safe static's placement new
#include <stdint.h> // for uint64_t under Linux/GCC
#include <utility> // std::move
struct foo
{
foo();
Expand Down
1 change: 1 addition & 0 deletions tests/Issue369_2.expect
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <new> // for thread-safe static's placement new
#include <stdint.h> // for uint64_t under Linux/GCC
#include <utility> // std::move
struct foo
{
foo();
Expand Down

0 comments on commit 2f6888e

Please sign in to comment.