[cfe-dev] How to set the byte alignment of struct?

huc1985 huc1985 at 163.com
Thu May 16 19:34:04 PDT 2013


Hi all!

I'm writing a static checker using clang AST. I want set struct alignment
manually. How to do this?

Thanks!

This is my top level:

    DiagnosticOptions* diagnosticOptions = new DiagnosticOptions;

    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagIDs(new
DiagnosticIDs());

    TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
    DiagnosticsEngine* diagnostics = new DiagnosticsEngine(diagIDs,
diagnosticOptions, DiagsBuffer);

    LangOptions languageOptions;

    FileSystemOptions fileSystemOptions;
    FileManager fileManager(fileSystemOptions);    

    SourceManager* sourceManager = new SourceManager(*diagnostics,
fileManager);

    TargetOptions* targetOptions = new TargetOptions;	
    targetOptions->Triple = llvm::sys::getDefaultTargetTriple();	
    TargetInfo *pTargetInfo =
clang::TargetInfo::CreateTargetInfo(*diagnostics, targetOptions);

    IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts(new
HeaderSearchOptions());

    HeaderSearch headerSearch(HSOpts, fileManager, *diagnostics,
languageOptions, pTargetInfo);

    for (vector<string>::const_iterator it =
filefold.begin();it!=filefold.end();it++)
    {
        (*HSOpts).AddPath(*it,frontend::Quoted,false,false);
    }
    ApplyHeaderSearchOptions(headerSearch, *HSOpts, languageOptions,
pTargetInfo->getTriple());

    //为该源文件创建一个clang的编译器实例
    OwningPtr<CompilerInstance> Clang(new CompilerInstance());

    PreprocessorOptions *preprocessorOptions = new PreprocessorOptions();

    Preprocessor *PP = new Preprocessor(preprocessorOptions, *diagnostics,
languageOptions
        , pTargetInfo, *sourceManager, headerSearch, *(Clang.get()));

    for (int i=0;i<(int)compile_define.size();i++){
        preprocessorOptions->addMacroDef(compile_define[i]);
    }

    FrontendOptions frontendOptions;
    InitializePreprocessor(*PP,  *preprocessorOptions, *HSOpts,
frontendOptions);

    const FileEntry *pFile = fileManager.getFile(file_name);
    sourceManager->createMainFileID(pFile);

    //为该编译器实例初始化。
    Clang->setDiagnostics(diagnostics);
    Clang->getDiagnosticClient().BeginSourceFile(Clang->getLangOpts(),PP);
    Clang->setPreprocessor(PP);
    Clang->setSourceManager(sourceManager);
    Clang->setTarget(pTargetInfo);
    SourceManager &sourceMgr = Clang->getSourceManager();
    Clang->createASTContext();
    Rewriter rewriter;
    rewriter.setSourceMgr(sourceMgr,languageOptions);

    MemoryMonitorASTConsumer consumer(rewriter,
MallocClangReport::GetInstance());
    consumer.mVisitor.SetCheckFunctionName(function);
    consumer.mVisitor.SetCheckFunctionArgumentIndex(arg_index);

    //AST解析
    ParseAST(*PP, &consumer, Clang->getASTContext(),false);

    Clang->getDiagnosticClient().EndSourceFile();



--
View this message in context: http://clang-developers.42468.n3.nabble.com/How-to-set-the-byte-alignment-of-struct-tp4032152.html
Sent from the Clang Developers mailing list archive at Nabble.com.




More information about the cfe-dev mailing list