llvm extract struct elements and struct size in C++

LLVM Newbie here. I have the following C++ program

using namespace std;
struct A{
  int i;
  int j;
};

int main()
{
   struct A obj;
   obj.i = 10;
   obj.j = obj.i;
   return 0;
}

Using clang++, I can see that LLVM IR contains struct field as below

%struct.A = type { i32, i32 }

I would like to obtain the structure elements using LLVM Pass. I write
the following program - that iterates through both global variables,
and each of the Instruction operands, but none of them help me in
extracting struct A or A.i or A.j.

    #include "llvm/Pass.h"
    #include "llvm/IR/Function.h"
    #include "llvm/Support/raw_ostream.h"

    #include <llvm/IR/Constants.h>
    #include <llvm/IR/DerivedTypes.h>
    #include <llvm/IR/Instructions.h>
    #include <llvm/IR/IntrinsicInst.h>
    #include <llvm/IR/LLVMContext.h>
    #include <llvm/IR/Module.h>

    #include <iostream>
    #include <map>
    #include <vector>

    using namespace llvm;

    namespace {

    class StructModulePass: public ModulePass {
    public:
    static char ID;
    StructModulePass() : ModulePass(ID) {}
    virtual bool runOnModule(Module &M1) override {
    // iterate over global structures
    M = &M1;
    int i;
    for(auto G = M->global_begin(); G!= M->global_end() ; G++, i++){
    errs() << i << " == > " ;
    errs().write_escaped(G->getName()) << "\n";
  }

// iterate through each instruction. module->function->BB->Inst
  for(auto &F_ : M->functions()){
    F = &F_;
    for(auto &B_ : *F)
      B = &B_;
      for(auto &I : *B) {
        for (unsigned i = 0; i < I.getNumOperands(); i++)
          std::cerr << I.getOperand(i)->getName().data() << std::endl;
      }
  }
  return true;
  }
private:
  Module *M;
  Function *F;
  BasicBlock *B;
};
  }

char StructModulePass:: ID = 0;
static RegisterPass<StructModulePass> X("getstructnamesize", "Get All
Struct Names and Sizes",
                             false /* Only looks at CFG */ ,
                             false /* Analysis Pass */);

I want to create a database of all structures (global and local)
defined and being used in my program. Eg. < A , <int32, int32> , B ,
<int32, bool , char *>>.

I have gone through doxygen pages, LLVM tutorials and checked if we
can get the struct values, but I am unable to find a way to extract
the structures without already knowing the struct values - eg.
creating an IRBuilder, inserting predefined IntTy32 type variables.
Any help in this regard or some relevant tutorials will help

I think the closest LLVM has is either
Module::getIdentifiedStructTypes or the class it actually uses:
"TypeFinder". Beware though, struct types may not have a name, and
even if they do it won't necessarily correspond to the original C or
C++ name in any meaningful way.

These both return an std::vector<StructType *> in some form, which you
can probe for the fields (they'll definitely be unnamed).

The other detail that's not obvious is that it looks like TypeFinder
probes nested structs, without requiring an explicit use of that
subtype within the modules. E.g. it would return [{i32, {i32}}, {i32}]
for something trivial like:

    define {i32, {i32}} @foo() { ret {i32, {i32}} undef }

Cheers

Tim.

Thank you,

This information was very useful.