[llvm] e3b5254 - [MC] Avoid calling vector<FieldInfo> members before FieldInfo is defined
Ilya Biryukov via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 10 10:22:48 PDT 2022
Author: Ilya Biryukov
Date: 2022-08-10T19:20:09+02:00
New Revision: e3b5254d7160abde7e6302548edca2cad8a6b63f
URL: https://github.com/llvm/llvm-project/commit/e3b5254d7160abde7e6302548edca2cad8a6b63f
DIFF: https://github.com/llvm/llvm-project/commit/e3b5254d7160abde7e6302548edca2cad8a6b63f.diff
LOG: [MC] Avoid calling vector<FieldInfo> members before FieldInfo is defined
The C++ Standard requires a complete type T when using any members of
`vector<T>`, see
https://eel.is/c++draft/vector#overview-4.
This only breaks with latest libc++ in C++20 mode and does not show up
in common configurations.
We have an internal experimental configuration that discovered this.
Reviewed By: alexfh
Differential Revision: https://reviews.llvm.org/D131595
Added:
Modified:
llvm/lib/MC/MCParser/MasmParser.cpp
Removed:
################################################################################
diff --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp
index 76533b2917cf..4f2a74e12265 100644
--- a/llvm/lib/MC/MCParser/MasmParser.cpp
+++ b/llvm/lib/MC/MCParser/MasmParser.cpp
@@ -140,9 +140,7 @@ struct StructInfo {
unsigned FieldAlignmentSize);
StructInfo() = default;
-
- StructInfo(StringRef StructName, bool Union, unsigned AlignmentValue)
- : Name(StructName), IsUnion(Union), Alignment(AlignmentValue) {}
+ StructInfo(StringRef StructName, bool Union, unsigned AlignmentValue);
};
// FIXME: This should probably use a class hierarchy, raw pointers between the
@@ -170,14 +168,8 @@ struct StructFieldInfo {
StructInfo Structure;
StructFieldInfo() = default;
- StructFieldInfo(const std::vector<StructInitializer> &V, StructInfo S) {
- Initializers = V;
- Structure = S;
- }
- StructFieldInfo(std::vector<StructInitializer> &&V, StructInfo S) {
- Initializers = V;
- Structure = S;
- }
+ StructFieldInfo(const std::vector<StructInitializer> &V, StructInfo S);
+ StructFieldInfo(std::vector<StructInitializer> &&V, StructInfo S);
};
class FieldInitializer {
@@ -189,133 +181,19 @@ class FieldInitializer {
StructFieldInfo StructInfo;
};
- ~FieldInitializer() {
- switch (FT) {
- case FT_INTEGRAL:
- IntInfo.~IntFieldInfo();
- break;
- case FT_REAL:
- RealInfo.~RealFieldInfo();
- break;
- case FT_STRUCT:
- StructInfo.~StructFieldInfo();
- break;
- }
- }
-
- FieldInitializer(FieldType FT) : FT(FT) {
- switch (FT) {
- case FT_INTEGRAL:
- new (&IntInfo) IntFieldInfo();
- break;
- case FT_REAL:
- new (&RealInfo) RealFieldInfo();
- break;
- case FT_STRUCT:
- new (&StructInfo) StructFieldInfo();
- break;
- }
- }
-
- FieldInitializer(SmallVector<const MCExpr *, 1> &&Values) : FT(FT_INTEGRAL) {
- new (&IntInfo) IntFieldInfo(Values);
- }
-
- FieldInitializer(SmallVector<APInt, 1> &&AsIntValues) : FT(FT_REAL) {
- new (&RealInfo) RealFieldInfo(AsIntValues);
- }
+ ~FieldInitializer();
+ FieldInitializer(FieldType FT);
+ FieldInitializer(SmallVector<const MCExpr *, 1> &&Values);
+ FieldInitializer(SmallVector<APInt, 1> &&AsIntValues);
FieldInitializer(std::vector<StructInitializer> &&Initializers,
- struct StructInfo Structure)
- : FT(FT_STRUCT) {
- new (&StructInfo) StructFieldInfo(Initializers, Structure);
- }
-
- FieldInitializer(const FieldInitializer &Initializer) : FT(Initializer.FT) {
- switch (FT) {
- case FT_INTEGRAL:
- new (&IntInfo) IntFieldInfo(Initializer.IntInfo);
- break;
- case FT_REAL:
- new (&RealInfo) RealFieldInfo(Initializer.RealInfo);
- break;
- case FT_STRUCT:
- new (&StructInfo) StructFieldInfo(Initializer.StructInfo);
- break;
- }
- }
-
- FieldInitializer(FieldInitializer &&Initializer) : FT(Initializer.FT) {
- switch (FT) {
- case FT_INTEGRAL:
- new (&IntInfo) IntFieldInfo(Initializer.IntInfo);
- break;
- case FT_REAL:
- new (&RealInfo) RealFieldInfo(Initializer.RealInfo);
- break;
- case FT_STRUCT:
- new (&StructInfo) StructFieldInfo(Initializer.StructInfo);
- break;
- }
- }
+ struct StructInfo Structure);
- FieldInitializer &operator=(const FieldInitializer &Initializer) {
- if (FT != Initializer.FT) {
- switch (FT) {
- case FT_INTEGRAL:
- IntInfo.~IntFieldInfo();
- break;
- case FT_REAL:
- RealInfo.~RealFieldInfo();
- break;
- case FT_STRUCT:
- StructInfo.~StructFieldInfo();
- break;
- }
- }
- FT = Initializer.FT;
- switch (FT) {
- case FT_INTEGRAL:
- IntInfo = Initializer.IntInfo;
- break;
- case FT_REAL:
- RealInfo = Initializer.RealInfo;
- break;
- case FT_STRUCT:
- StructInfo = Initializer.StructInfo;
- break;
- }
- return *this;
- }
+ FieldInitializer(const FieldInitializer &Initializer);
+ FieldInitializer(FieldInitializer &&Initializer);
- FieldInitializer &operator=(FieldInitializer &&Initializer) {
- if (FT != Initializer.FT) {
- switch (FT) {
- case FT_INTEGRAL:
- IntInfo.~IntFieldInfo();
- break;
- case FT_REAL:
- RealInfo.~RealFieldInfo();
- break;
- case FT_STRUCT:
- StructInfo.~StructFieldInfo();
- break;
- }
- }
- FT = Initializer.FT;
- switch (FT) {
- case FT_INTEGRAL:
- IntInfo = Initializer.IntInfo;
- break;
- case FT_REAL:
- RealInfo = Initializer.RealInfo;
- break;
- case FT_STRUCT:
- StructInfo = Initializer.StructInfo;
- break;
- }
- return *this;
- }
+ FieldInitializer &operator=(const FieldInitializer &Initializer);
+ FieldInitializer &operator=(FieldInitializer &&Initializer);
};
struct StructInitializer {
@@ -340,6 +218,22 @@ struct FieldInfo {
FieldInfo(FieldType FT) : Contents(FT) {}
};
+StructFieldInfo::StructFieldInfo(const std::vector<StructInitializer> &V,
+ StructInfo S) {
+ Initializers = V;
+ Structure = S;
+}
+
+StructFieldInfo::StructFieldInfo(std::vector<StructInitializer> &&V,
+ StructInfo S) {
+ Initializers = V;
+ Structure = S;
+}
+
+StructInfo::StructInfo(StringRef StructName, bool Union,
+ unsigned AlignmentValue)
+ : Name(StructName), IsUnion(Union), Alignment(AlignmentValue) {}
+
FieldInfo &StructInfo::addField(StringRef FieldName, FieldType FT,
unsigned FieldAlignmentSize) {
if (!FieldName.empty())
@@ -355,6 +249,139 @@ FieldInfo &StructInfo::addField(StringRef FieldName, FieldType FT,
return Field;
}
+FieldInitializer::~FieldInitializer() {
+ switch (FT) {
+ case FT_INTEGRAL:
+ IntInfo.~IntFieldInfo();
+ break;
+ case FT_REAL:
+ RealInfo.~RealFieldInfo();
+ break;
+ case FT_STRUCT:
+ StructInfo.~StructFieldInfo();
+ break;
+ }
+}
+
+FieldInitializer::FieldInitializer(FieldType FT) : FT(FT) {
+ switch (FT) {
+ case FT_INTEGRAL:
+ new (&IntInfo) IntFieldInfo();
+ break;
+ case FT_REAL:
+ new (&RealInfo) RealFieldInfo();
+ break;
+ case FT_STRUCT:
+ new (&StructInfo) StructFieldInfo();
+ break;
+ }
+}
+
+FieldInitializer::FieldInitializer(SmallVector<const MCExpr *, 1> &&Values)
+ : FT(FT_INTEGRAL) {
+ new (&IntInfo) IntFieldInfo(Values);
+}
+
+FieldInitializer::FieldInitializer(SmallVector<APInt, 1> &&AsIntValues)
+ : FT(FT_REAL) {
+ new (&RealInfo) RealFieldInfo(AsIntValues);
+}
+
+FieldInitializer::FieldInitializer(
+ std::vector<StructInitializer> &&Initializers, struct StructInfo Structure)
+ : FT(FT_STRUCT) {
+ new (&StructInfo) StructFieldInfo(Initializers, Structure);
+}
+
+FieldInitializer::FieldInitializer(const FieldInitializer &Initializer)
+ : FT(Initializer.FT) {
+ switch (FT) {
+ case FT_INTEGRAL:
+ new (&IntInfo) IntFieldInfo(Initializer.IntInfo);
+ break;
+ case FT_REAL:
+ new (&RealInfo) RealFieldInfo(Initializer.RealInfo);
+ break;
+ case FT_STRUCT:
+ new (&StructInfo) StructFieldInfo(Initializer.StructInfo);
+ break;
+ }
+}
+
+FieldInitializer::FieldInitializer(FieldInitializer &&Initializer)
+ : FT(Initializer.FT) {
+ switch (FT) {
+ case FT_INTEGRAL:
+ new (&IntInfo) IntFieldInfo(Initializer.IntInfo);
+ break;
+ case FT_REAL:
+ new (&RealInfo) RealFieldInfo(Initializer.RealInfo);
+ break;
+ case FT_STRUCT:
+ new (&StructInfo) StructFieldInfo(Initializer.StructInfo);
+ break;
+ }
+}
+
+FieldInitializer &
+FieldInitializer::operator=(const FieldInitializer &Initializer) {
+ if (FT != Initializer.FT) {
+ switch (FT) {
+ case FT_INTEGRAL:
+ IntInfo.~IntFieldInfo();
+ break;
+ case FT_REAL:
+ RealInfo.~RealFieldInfo();
+ break;
+ case FT_STRUCT:
+ StructInfo.~StructFieldInfo();
+ break;
+ }
+ }
+ FT = Initializer.FT;
+ switch (FT) {
+ case FT_INTEGRAL:
+ IntInfo = Initializer.IntInfo;
+ break;
+ case FT_REAL:
+ RealInfo = Initializer.RealInfo;
+ break;
+ case FT_STRUCT:
+ StructInfo = Initializer.StructInfo;
+ break;
+ }
+ return *this;
+}
+
+FieldInitializer &FieldInitializer::operator=(FieldInitializer &&Initializer) {
+ if (FT != Initializer.FT) {
+ switch (FT) {
+ case FT_INTEGRAL:
+ IntInfo.~IntFieldInfo();
+ break;
+ case FT_REAL:
+ RealInfo.~RealFieldInfo();
+ break;
+ case FT_STRUCT:
+ StructInfo.~StructFieldInfo();
+ break;
+ }
+ }
+ FT = Initializer.FT;
+ switch (FT) {
+ case FT_INTEGRAL:
+ IntInfo = Initializer.IntInfo;
+ break;
+ case FT_REAL:
+ RealInfo = Initializer.RealInfo;
+ break;
+ case FT_STRUCT:
+ StructInfo = Initializer.StructInfo;
+ break;
+ }
+ return *this;
+}
+
/// The concrete assembly parser instance.
// Note that this is a full MCAsmParser, not an MCAsmParserExtension!
// It's a peer of AsmParser, not of COFFAsmParser, WasmAsmParser, etc.
More information about the llvm-commits
mailing list