llvm/clang/test/Modules/compare-record.c

// RUN: rm -rf %t
// RUN: split-file %s %t

// Build first header file
// RUN: echo "#define FIRST" >> %t/include/first.h
// RUN: cat %t/test.c        >> %t/include/first.h
// RUN: echo "#undef FIRST"  >> %t/include/first.h

// Build second header file
// RUN: echo "#define SECOND" >> %t/include/second.h
// RUN: cat %t/test.c         >> %t/include/second.h
// RUN: echo "#undef SECOND"  >> %t/include/second.h

// Test that each header can compile
// RUN: %clang_cc1 -fsyntax-only -x objective-c %t/include/first.h -fblocks -fobjc-arc
// RUN: %clang_cc1 -fsyntax-only -x objective-c %t/include/second.h -fblocks -fobjc-arc

// Run test
// RUN: %clang_cc1 -I%t/include -verify %t/test.c -fblocks -fobjc-arc \
// RUN:            -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/modules.cache

// Run tests for nested structs
// DEFINE: %{filename} = test-nested-struct.c
// DEFINE: %{macro_flag} = -DCASE1=1
// DEFINE: %{command} = %clang_cc1 -I%t/include -verify %t/%{filename} -fblocks -fobjc-arc \
// DEFINE:             -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/modules.cache \
// DEFINE:             %{macro_flag} -emit-llvm -o %t/%{filename}.bc
// RUN: %{command}
// REDEFINE: %{macro_flag} = -DCASE2=1
// RUN: %{command}
// REDEFINE: %{macro_flag} = -DCASE3=1
// RUN: %{command}

// Run tests for anonymous nested structs and unions
// REDEFINE: %{filename} = test-anonymous.c
// REDEFINE: %{macro_flag} = -DCASE1=1
// RUN: %{command}
// REDEFINE: %{macro_flag} = -DCASE2=1
// RUN: %{command}
// REDEFINE: %{macro_flag} = -DCASE3=1
// RUN: %{command}

// Test that we don't accept different structs and unions with the same name
// from multiple modules but detect mismatches and provide actionable
// diagnostic.

//--- include/first-empty.h
//--- include/module.modulemap
module First {
  module Empty {
    header "first-empty.h"
  }
  module Hidden {
    header "first.h"
    header "first-nested-struct.h"
    header "first-anonymous.h"
    export *
  }
}
module Second {
  header "second.h"
  header "second-nested-struct.h"
  header "second-anonymous.h"
  export *
}

//--- test.c
#if !defined(FIRST) && !defined(SECOND)
# include "first-empty.h"
# include "second.h"
#endif

#if defined(FIRST)
struct CompareForwardDeclaration1;
struct CompareForwardDeclaration2 {};
#elif defined(SECOND)
struct CompareForwardDeclaration1 {};
struct CompareForwardDeclaration2;
#else
struct CompareForwardDeclaration1 *compareForwardDeclaration1;
struct CompareForwardDeclaration2 *compareForwardDeclaration2;
#endif

#if defined(FIRST)
struct CompareMatchingFields {
  int matchingFieldName;
};

struct CompareFieldPresence1 {
  int fieldPresence1;
};
struct CompareFieldPresence2 {};

struct CompareFieldName {
  int fieldNameA;
};

struct CompareFieldOrder {
  int fieldOrderX;
  int fieldOrderY;
};
#elif defined(SECOND)
struct CompareMatchingFields {
  int matchingFieldName;
};

struct CompareFieldPresence1 {
};
struct CompareFieldPresence2 {
  int fieldPresence2;
};

struct CompareFieldName {
  int fieldNameB;
};

struct CompareFieldOrder {
  int fieldOrderY;
  int fieldOrderX;
};
#else
struct CompareMatchingFields compareMatchingFields;
struct CompareFieldPresence1 compareFieldPresence1;
// [email protected]:* {{'CompareFieldPresence1' has different definitions in different modules; first difference is definition in module 'First.Hidden' found field}}
// [email protected]:* {{but in 'Second' found end of class}}
struct CompareFieldPresence2 compareFieldPresence2;
// [email protected]:* {{'CompareFieldPresence2::fieldPresence2' from module 'Second' is not present in definition of 'struct CompareFieldPresence2' in module 'First.Hidden'}}
// [email protected]:* {{definition has no member 'fieldPresence2'}}
struct CompareFieldName compareFieldName;
// [email protected]:* {{'CompareFieldName::fieldNameB' from module 'Second' is not present in definition of 'struct CompareFieldName' in module 'First.Hidden'}}
// [email protected]:* {{definition has no member 'fieldNameB'}}
struct CompareFieldOrder compareFieldOrder;
// [email protected]:* {{'CompareFieldOrder' has different definitions in different modules; first difference is definition in module 'First.Hidden' found field 'fieldOrderX'}}
// [email protected]:* {{but in 'Second' found field 'fieldOrderY'}}
#endif

#if defined(FIRST)
struct CompareFieldType {
  int fieldType;
};

typedef int FieldTypedefNameA;
struct CompareFieldTypedefName {
  FieldTypedefNameA fieldTypedefName;
};

typedef int TypedefUnderlyingType;
struct CompareFieldTypeUnderlyingTypedef {
  TypedefUnderlyingType fieldTypeUnderlyingTypedef;
};

typedef int TypedefFinal;
struct CompareFieldTypedefChain {
  TypedefFinal fieldTypeTypedefChain;
};
#elif defined(SECOND)
struct CompareFieldType {
  float fieldType;
};

typedef int FieldTypedefNameB;
struct CompareFieldTypedefName {
  FieldTypedefNameB fieldTypedefName;
};

struct CompareFieldTypeUnderlyingTypedef {
  int fieldTypeUnderlyingTypedef;
};

typedef int TypedefIntermediate;
typedef TypedefIntermediate TypedefFinal;
struct CompareFieldTypedefChain {
  TypedefFinal fieldTypeTypedefChain;
};
#else
struct CompareFieldType compareFieldType;
// [email protected]:* {{'CompareFieldType::fieldType' from module 'Second' is not present in definition of 'struct CompareFieldType' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'fieldType' does not match}}
struct CompareFieldTypedefName compareFieldTypedefName;
// [email protected]:* {{'CompareFieldTypedefName' has different definitions in different modules; first difference is definition in module 'First.Hidden' found field 'fieldTypedefName' with type 'FieldTypedefNameA' (aka 'int')}}
// [email protected]:* {{but in 'Second' found field 'fieldTypedefName' with type 'FieldTypedefNameB' (aka 'int')}}
struct CompareFieldTypeUnderlyingTypedef compareFieldTypeUnderlyingTypedef;
// [email protected]:* {{'CompareFieldTypeUnderlyingTypedef' has different definitions in different modules; first difference is definition in module 'First.Hidden' found field 'fieldTypeUnderlyingTypedef' with type 'TypedefUnderlyingType' (aka 'int')}}
// [email protected]:* {{but in 'Second' found field 'fieldTypeUnderlyingTypedef' with type 'int'}}
struct CompareFieldTypedefChain compareFieldTypedefChain;
#endif

#if defined(FIRST)
struct CompareMatchingBitfields {
  unsigned matchingBitfields : 3;
};

struct CompareBitfieldPresence1 {
  unsigned bitfieldPresence1 : 1;
};
struct CompareBitfieldPresence2 {
  unsigned bitfieldPresence2;
};

struct CompareBitfieldWidth {
  unsigned bitfieldWidth : 2;
};

struct CompareBitfieldWidthExpression {
  unsigned bitfieldWidthExpression : 1 + 1;
};
#elif defined(SECOND)
struct CompareMatchingBitfields {
  unsigned matchingBitfields : 3;
};

struct CompareBitfieldPresence1 {
  unsigned bitfieldPresence1;
};
struct CompareBitfieldPresence2 {
  unsigned bitfieldPresence2 : 1;
};

struct CompareBitfieldWidth {
  unsigned bitfieldWidth : 1;
};

struct CompareBitfieldWidthExpression {
  unsigned bitfieldWidthExpression : 2;
};
#else
struct CompareMatchingBitfields compareMatchingBitfields;
struct CompareBitfieldPresence1 compareBitfieldPresence1;
// [email protected]:* {{'CompareBitfieldPresence1' has different definitions in different modules; first difference is definition in module 'First.Hidden' found bitfield 'bitfieldPresence1'}}
// [email protected]:* {{but in 'Second' found non-bitfield 'bitfieldPresence1'}}
struct CompareBitfieldPresence2 compareBitfieldPresence2;
// [email protected]:* {{'CompareBitfieldPresence2' has different definitions in different modules; first difference is definition in module 'First.Hidden' found non-bitfield 'bitfieldPresence2'}}
// [email protected]:* {{but in 'Second' found bitfield 'bitfieldPresence2'}}
struct CompareBitfieldWidth compareBitfieldWidth;
// [email protected]:* {{'CompareBitfieldWidth' has different definitions in different modules; first difference is definition in module 'First.Hidden' found bitfield 'bitfieldWidth' with one width expression}}
// [email protected]:* {{but in 'Second' found bitfield 'bitfieldWidth' with different width expression}}
struct CompareBitfieldWidthExpression compareBitfieldWidthExpression;
// [email protected]:* {{'CompareBitfieldWidthExpression' has different definitions in different modules; first difference is definition in module 'First.Hidden' found bitfield 'bitfieldWidthExpression' with one width expression}}
// [email protected]:* {{but in 'Second' found bitfield 'bitfieldWidthExpression' with different width expressio}}
#endif

#if defined(FIRST)
struct CompareMatchingArrayFields {
  int matchingArrayField[7];
};

struct CompareArrayLength {
  int arrayLengthField[5];
};

struct CompareArrayType {
  int arrayTypeField[5];
};
#elif defined(SECOND)
struct CompareMatchingArrayFields {
  int matchingArrayField[7];
};

struct CompareArrayLength {
  int arrayLengthField[7];
};

struct CompareArrayType {
  float arrayTypeField[5];
};
#else
struct CompareMatchingArrayFields compareMatchingArrayFields;
struct CompareArrayLength compareArrayLength;
// [email protected]:* {{'CompareArrayLength::arrayLengthField' from module 'Second' is not present in definition of 'struct CompareArrayLength' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'arrayLengthField' does not match}}
struct CompareArrayType compareArrayType;
// [email protected]:* {{'CompareArrayType::arrayTypeField' from module 'Second' is not present in definition of 'struct CompareArrayType' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'arrayTypeField' does not match}}
#endif

#if defined(FIRST)
struct CompareFieldAsForwardDeclaration {
  struct FieldForwardDeclaration *fieldForwardDeclaration;
};

enum FieldEnumA { kFieldEnumValue };
struct CompareFieldAsEnum {
  enum FieldEnumA fieldEnum;
};

struct FieldStructA {};
struct CompareFieldAsStruct {
  struct FieldStructA fieldStruct;
};
#elif defined(SECOND)
struct FieldForwardDeclaration {};
struct CompareFieldAsForwardDeclaration {
  struct FieldForwardDeclaration *fieldForwardDeclaration;
};

enum FieldEnumB { kFieldEnumValue };
struct CompareFieldAsEnum {
  enum FieldEnumB fieldEnum;
};

struct FieldStructB {};
struct CompareFieldAsStruct {
  struct FieldStructB fieldStruct;
};
#else
struct CompareFieldAsForwardDeclaration compareFieldAsForwardDeclaration;
struct CompareFieldAsEnum compareFieldAsEnum;
// [email protected]:* {{'CompareFieldAsEnum::fieldEnum' from module 'Second' is not present in definition of 'struct CompareFieldAsEnum' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'fieldEnum' does not match}}
struct CompareFieldAsStruct compareFieldAsStruct;
// [email protected]:* {{'CompareFieldAsStruct::fieldStruct' from module 'Second' is not present in definition of 'struct CompareFieldAsStruct' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'fieldStruct' does not match}}
#endif

#if defined(FIRST)
union CompareMatchingUnionFields {
  int matchingFieldA;
  float matchingFieldB;
};

union CompareUnionFieldOrder {
  int unionFieldOrderA;
  float unionFieldOrderB;
};

union CompareUnionFieldType {
  int unionFieldType;
};
#elif defined(SECOND)
union CompareMatchingUnionFields {
  int matchingFieldA;
  float matchingFieldB;
};

union CompareUnionFieldOrder {
  float unionFieldOrderB;
  int unionFieldOrderA;
};

union CompareUnionFieldType {
  unsigned int unionFieldType;
};
#else
union CompareMatchingUnionFields compareMatchingUnionFields;
union CompareUnionFieldOrder compareUnionFieldOrder;
// [email protected]:* {{'CompareUnionFieldOrder' has different definitions in different modules; first difference is definition in module 'First.Hidden' found field 'unionFieldOrderA'}}
// [email protected]:* {{but in 'Second' found field 'unionFieldOrderB'}}
union CompareUnionFieldType compareUnionFieldType;
// [email protected]:* {{'CompareUnionFieldType::unionFieldType' from module 'Second' is not present in definition of 'union CompareUnionFieldType' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'unionFieldType' does not match}}
#endif

// Test that we find and compare definitions even if they are not the first encountered declaration in a module.
#if defined(FIRST)
struct CompareDefinitionsRegardlessForwardDeclarations {
  int definitionField;
};
#elif defined(SECOND)
struct CompareDefinitionsRegardlessForwardDeclarations;
struct CompareDefinitionsRegardlessForwardDeclarations {
  float definitionField;
};
#else
struct CompareDefinitionsRegardlessForwardDeclarations compareDefinitions;
// [email protected]:* {{'CompareDefinitionsRegardlessForwardDeclarations::definitionField' from module 'Second' is not present in definition of 'struct CompareDefinitionsRegardlessForwardDeclarations' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'definitionField' does not match}}
#endif

//--- include/first-nested-struct.h
struct CompareNestedStruct {
  struct NestedLevel1 {
    struct NestedLevel2 {
      int a;
    } y;
  } x;
};

struct IndirectStruct {
  int mismatchingField;
};
struct DirectStruct {
  struct IndirectStruct indirectField;
};
struct CompareDifferentFieldInIndirectStruct {
  struct DirectStruct directField;
};
struct CompareIndirectStructPointer {
  struct DirectStruct *directFieldPointer;
};

//--- include/second-nested-struct.h
struct CompareNestedStruct {
  struct NestedLevel1 {
    struct NestedLevel2 {
      float b;
    } y;
  } x;
};

struct IndirectStruct {
  float mismatchingField;
};
struct DirectStruct {
  struct IndirectStruct indirectField;
};
struct CompareDifferentFieldInIndirectStruct {
  struct DirectStruct directField;
};
struct CompareIndirectStructPointer {
  struct DirectStruct *directFieldPointer;
};

//--- test-nested-struct.c
#include "first-empty.h"
#include "second-nested-struct.h"

#if defined(CASE1)
struct CompareNestedStruct compareNestedStruct;
// [email protected]:* {{'NestedLevel2::b' from module 'Second' is not present in definition of 'struct NestedLevel2' in module 'First.Hidden'}}
// [email protected]:* {{definition has no member 'b'}}
#elif defined(CASE2)
struct CompareDifferentFieldInIndirectStruct compareIndirectStruct;
// [email protected]:* {{'IndirectStruct::mismatchingField' from module 'Second' is not present in definition of 'struct IndirectStruct' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'mismatchingField' does not match}}
#elif defined(CASE3)
// [email protected]:* {{'IndirectStruct::mismatchingField' from module 'Second' is not present in definition of 'struct IndirectStruct' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'mismatchingField' does not match}}
struct CompareIndirectStructPointer compareIndirectStructPointer;
struct DirectStruct test() {
  // Make sure the type behind the pointer is inspected.
  return *compareIndirectStructPointer.directFieldPointer;
}
#endif

//--- include/first-anonymous.h
struct CompareAnonymousNestedUnion {
  union {
    int anonymousNestedUnionField;
  };
};

struct CompareAnonymousNestedStruct {
  struct {
    int anonymousNestedStructField;
  };
};

struct CompareDeeplyNestedAnonymousUnionsAndStructs {
  union {
    int x;
    union {
      int y;
      struct {
        int z;
      };
    };
  };
};

//--- include/second-anonymous.h
struct CompareAnonymousNestedUnion {
  union {
    float anonymousNestedUnionField;
  };
};

struct CompareAnonymousNestedStruct {
  struct {
    float anonymousNestedStructField;
  };
};

struct CompareDeeplyNestedAnonymousUnionsAndStructs {
  union {
    int x;
    union {
      int y;
      struct {
        float z;
      };
    };
  };
};

//--- test-anonymous.c
#include "first-empty.h"
#include "second-anonymous.h"

#if defined(CASE1)
struct CompareAnonymousNestedUnion compareAnonymousNestedUnion;
// [email protected]:* {{'CompareAnonymousNestedUnion::(anonymous union)::anonymousNestedUnionField' from module 'Second' is not present in definition of 'union CompareAnonymousNestedUnion::(anonymous at {{.*}})' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'anonymousNestedUnionField' does not match}}
#elif defined(CASE2)
struct CompareAnonymousNestedStruct compareAnonymousNestedStruct;
// [email protected]:* {{'CompareAnonymousNestedStruct::(anonymous struct)::anonymousNestedStructField' from module 'Second' is not present in definition of 'struct CompareAnonymousNestedStruct::(anonymous at {{.*}})' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'anonymousNestedStructField' does not match}}
#elif defined(CASE3)
struct CompareDeeplyNestedAnonymousUnionsAndStructs compareDeeplyNested;
// [email protected]:* {{'CompareDeeplyNestedAnonymousUnionsAndStructs::(anonymous union)::(anonymous union)::(anonymous struct)::z' from module 'Second' is not present in definition of 'struct CompareDeeplyNestedAnonymousUnionsAndStructs::(anonymous at {{.*}})' in module 'First.Hidden'}}
// [email protected]:* {{declaration of 'z' does not match}}
#endif