1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-20 15:35:24 +02:00

Bug 457770 - Size of empty structure

Change-Id: Id6f02228968986090867b2053c7815e594a404c1
Signed-off-by: Nathan Ridge <zeratul976@hotmail.com>
This commit is contained in:
Nathan Ridge 2015-02-05 02:16:18 -05:00 committed by Gerrit Code Review @ Eclipse.org
parent 8a896c57ed
commit 327709c58e
2 changed files with 13 additions and 2 deletions

View file

@ -21,6 +21,7 @@ import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.LVALUE;
import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.XVALUE; import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.XVALUE;
import static org.eclipse.cdt.core.parser.ParserLanguage.CPP; import static org.eclipse.cdt.core.parser.ParserLanguage.CPP;
import static org.eclipse.cdt.core.parser.tests.VisibilityAsserts.assertVisibility; import static org.eclipse.cdt.core.parser.tests.VisibilityAsserts.assertVisibility;
import static org.junit.Assert.assertNotEquals;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@ -9079,6 +9080,15 @@ public class AST2CPPTests extends AST2TestBase {
long BSize = SizeofCalculator.getSizeAndAlignment(B, nameB).size; long BSize = SizeofCalculator.getSizeAndAlignment(B, nameB).size;
assertEquals(pointerSize, BSize); assertEquals(pointerSize, BSize);
} }
// struct waldo {};
public void testSizeofEmptyStruct_457770() throws Exception {
BindingAssertionHelper bh = getAssertionHelper();
IASTName nameWaldo = bh.findName("waldo");
ICPPClassType waldo = (ICPPClassType) nameWaldo.resolveBinding();
long waldoSize = SizeofCalculator.getSizeAndAlignment(waldo, nameWaldo).size;
assertNotEquals(0, waldoSize);
}
// template <bool> struct B {}; // template <bool> struct B {};
// template <> // template <>

View file

@ -329,8 +329,9 @@ public class SizeofCalculator {
if (maxAlignment < info.alignment) if (maxAlignment < info.alignment)
maxAlignment = info.alignment; maxAlignment = info.alignment;
} }
if (size > 0) if (size == 0) // a structure cannot have size 0
size += maxAlignment - (size - 1) % maxAlignment - 1; size = 1;
size += maxAlignment - (size - 1) % maxAlignment - 1;
return new SizeAndAlignment(size, maxAlignment); return new SizeAndAlignment(size, maxAlignment);
} }