Skip to content

Commit 8ee6ed1

Browse files
authored
Cleanup Windows code to conform to stricter C++ 17 compiler (#4804)
* Cleanup windows build to conform to C++ 17 MSVC's C++ language implementation deviates from the standard in a number of ways, this patch cleans up several issues that become errors if you build DXC using clang-cl, which has a more strict interpreteation of C++. This change should not have any functional impact on the final program. Specific changes include: * static_assert(false...) is removed, this is always ill-formed by the C++ standard. * Under template instantiations implicit `this` is not always allowed. * Character array `<` comparision is... probably not what was intended. * Implicit conversions are stricter than MSVC enforces. * Template base types need explicit type specialization when referred to. * It is illegal to initialize more than one member of a union. * Scope nested enums cannot be forward declared. * Header paths should be case-sensitive. * Exception specifications of specific types are disallowed in C++ 17. * Clang is stricter about const-correctness, especially as it relates to constant c strings. * Update based on review feedback. * Converted string construction to copy-constructor calls. * Removed constexpr method bodys to cause linker error. * Catching other methods I missed. * I was a bit too over-zealous deleting methods.
1 parent fb6b287 commit 8ee6ed1

20 files changed

Lines changed: 43 additions & 64 deletions

File tree

include/dxc/DxilContainer/DxilRuntimeReflection.h

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -237,39 +237,19 @@ class RawBytesReader {
237237
template<typename _T>
238238
class RecordTraits {
239239
public:
240-
static constexpr const char *TypeName() {
241-
#ifdef _WIN32
242-
static_assert(false, "");
243-
#else
244-
assert(false);
245-
#endif
246-
return nullptr;
247-
}
240+
static constexpr const char *TypeName();
248241

249-
static constexpr RuntimeDataPartType PartType() {
250-
#ifdef _WIN32
251-
static_assert(false, "");
252-
#else
253-
assert(false);
254-
#endif
255-
return RuntimeDataPartType::Invalid;
256-
}
242+
static constexpr RuntimeDataPartType PartType();
257243

258244
// If the following static assert is hit, it means a structure defined with
259245
// RDAT_STRUCT is being used in ref type, which requires the struct to have
260246
// a table and be defined with RDAT_STRUCT_TABLE instead.
261-
static constexpr RecordTableIndex TableIndex() {
262-
#ifdef _WIN32
263-
static_assert(false, "");
264-
#else
265-
assert(false);
266-
#endif
267-
return (RecordTableIndex)-1;
268-
}
247+
static constexpr RecordTableIndex TableIndex();
248+
269249
// RecordSize() is defined in order to allow for use of forward decl type in RecordRef
270-
static constexpr size_t RecordSize() { /*static_assert(false, "");*/ return sizeof(_T); }
250+
static constexpr size_t RecordSize() { return sizeof(_T); }
271251
static constexpr size_t MaxRecordSize() { return RecordTraits<_T>::DerivedRecordSize(); }
272-
static constexpr size_t DerivedRecordSize() { return sizeof(_T); }
252+
static constexpr size_t DerivedRecordSize();
273253
};
274254

275255
///////////////////////////////////////

include/dxc/Test/RDATDumper.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ void DumpWithBase(const hlsl::RDAT::RDATContext &ctx, DumpContext &d, const _Rec
4444
template<typename _RecordType>
4545
class RecordRefDumper : public hlsl::RDAT::RecordRef<_RecordType> {
4646
public:
47-
RecordRefDumper(uint32_t index) { Index = index; }
47+
RecordRefDumper(uint32_t index) { this->Index = index; }
4848
template<typename _DumpTy = _RecordType>
4949
const char *TypeName(const hlsl::RDAT::RDATContext &ctx) const {
50-
if (const char *name = RecordRefDumper<_DumpTy>(Index).TypeNameDerived(ctx))
50+
if (const char *name = RecordRefDumper<_DumpTy>(this->Index).TypeNameDerived(ctx))
5151
return name;
52-
RecordRef<_DumpTy> rr = { Index };
52+
RecordRef<_DumpTy> rr = { this->Index };
5353
if (rr.Get(ctx))
5454
return RecordTraits<_DumpTy>::TypeName();
5555
return nullptr;
5656
}
5757
template<typename _DumpTy = _RecordType>
5858
void Dump(const hlsl::RDAT::RDATContext &ctx, DumpContext &d) const {
59-
RecordRefDumper<_DumpTy> rrDumper(Index);
59+
RecordRefDumper<_DumpTy> rrDumper(this->Index);
6060
if (const _DumpTy *ptr = rrDumper.Get(ctx)) {
6161
static_cast< const RecordDumper<_DumpTy>* >(ptr)->Dump(ctx, d);
6262
rrDumper.DumpDerived(ctx, d);

lib/DxcSupport/dxcapi.use.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static std::string GetWin32ErrorMessage(DWORD err) {
3333
DWORD formattedMsgLen =
3434
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
3535
nullptr, err, 0, formattedMsg, _countof(formattedMsg), 0);
36-
if (formattedMsg > 0 && formattedMsgLen < _countof(formattedMsg)) {
36+
if (formattedMsgLen > 0 && formattedMsgLen < _countof(formattedMsg)) {
3737
TrimEOL(formattedMsg);
3838
return std::string(formattedMsg);
3939
}

lib/DxilDia/DxcPixTypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ STDMETHODIMP dxil_debug_info::DxcPixStructType::GetFieldByName(
275275
_In_ LPCWSTR lpName,
276276
_Outptr_result_z_ IDxcPixStructField **ppField)
277277
{
278-
std::string name = CW2A(lpName);
278+
std::string name = std::string(CW2A(lpName));
279279
for (auto *Node : m_pStruct->getElements())
280280
{
281281
auto* pDIField = llvm::dyn_cast<llvm::DIDerivedType>(Node);

lib/DxilDia/DxcPixVariables.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ STDMETHODIMP dxil_debug_info::DxcPixDxilLiveVariables::GetVariableByName(
208208
_In_ LPCWSTR Name,
209209
_Outptr_result_z_ IDxcPixVariable **ppVariable)
210210
{
211-
std::string name = CW2A(Name);
211+
std::string name = std::string(CW2A(Name));
212212

213213
for (auto *VarInfo : m_LiveVars)
214214
{

lib/DxilDia/DxilDiaSymbolManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ struct DISymbol : public Symbol {
103103

104104
template <typename N>
105105
struct TypedSymbol : public DISymbol<N> {
106-
TypedSymbol(IMalloc *M, N Node, DWORD dwTypeID, llvm::DIType *Type) : DISymbol(M, Node), m_dwTypeID(dwTypeID), m_pType(Type) {}
106+
TypedSymbol(IMalloc *M, N Node, DWORD dwTypeID, llvm::DIType *Type) : DISymbol<N>(M, Node), m_dwTypeID(dwTypeID), m_pType(Type) {}
107107

108108
STDMETHODIMP get_type(
109109
/* [retval][out] */ IDiaSymbol **ppRetVal) override {

lib/Support/Windows/Program.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ ErrorOr<std::string> sys::findProgramByName(StringRef Name,
9797

9898
return std::string(U8Result.begin(), U8Result.end());
9999
#else
100-
return "";
100+
return std::string("");
101101
#endif
102102
}
103103

projects/dxilconv/lib/DxbcConverter/DxbcConverterImpl.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,9 @@ class DxbcConverter : public IDxbcConverter {
407407
};
408408
vector<pair<unsigned, BasicBlock*> > SwitchCases; // Switch
409409

410-
Scope() : Kind(Kind::Function), pPreScopeBB(nullptr), pPostScopeBB(nullptr), NameIndex(0),
411-
pThenBB(nullptr), pElseBB(nullptr), pCond(nullptr),
412-
pLoopBB(nullptr), ContinueIndex(0), LoopBreakIndex(0),
413-
pDefaultBB(nullptr), pSelector(nullptr), CaseGroupIndex(0), SwitchBreakIndex(0),
414-
LabelIdx(0), CallIdx(0), ReturnTokenOffset(0), ReturnIndex(0), bEntryFunc(false),
415-
pHullLoopBB(nullptr), HullLoopBreakIndex(0), pInductionVar(nullptr), HullLoopTripCount(0) {}
410+
Scope() : Kind(Kind::Function), pPreScopeBB(nullptr), pPostScopeBB(nullptr), NameIndex(0) {
411+
memset(reinterpret_cast<char*>(&pThenBB), '\0', reinterpret_cast<char*>(&SwitchCases) - reinterpret_cast<char*>(&pThenBB));
412+
}
416413

417414
void SetEntry(bool b = true) { DXASSERT_NOMSG(Kind==Function); bEntryFunc = b; }
418415
bool IsEntry() const { DXASSERT_NOMSG(Kind==Function); return bEntryFunc; }

projects/dxilconv/lib/DxbcConverter/DxbcUtil.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
#include "dxc/DXIL/DxilResource.h"
2020
#include "dxc/DXIL/DxilConstants.h"
2121

22+
#include "llvm/IR/Instructions.h"
23+
2224
namespace llvm {
2325
class Type;
2426
class LLVMContext;
2527
class Value;
26-
class AtomicRMWInst;
27-
enum AtomicRMWInst::BinOp;
2828
}
2929

3030
#define DXASSERT_DXBC(__exp) DXASSERT(__exp, "otherwise incorrect assumption about DXBC")

projects/dxilconv/unittests/DxilConvTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ TEST_F(DxilConvTest, ManualFileCheckTest) {
188188
WEX::Common::String value;
189189
VERIFY_SUCCEEDED(RuntimeParameters::TryGetValue(L"InputPath", value));
190190

191-
std::wstring path = value;
191+
std::wstring path = static_cast<const wchar_t*>(value);
192192
if (!llvm::sys::path::is_absolute(CW2A(path.c_str()).m_psz)) {
193193
path = hlsl_test::GetPathToHlslDataFile(path.c_str());
194194
}

0 commit comments

Comments
 (0)