简体   繁体   中英

how to find symbols that should be exported

If you compile with -fvisibility=hidden or with msvc you have to export your shared library symbols manually. As an experiment, how could you find them automatically with AST matchers (clang-query)?

It's not that easy as a minimal set of export declarations is desired and things quickly get complicated with inline functions, templates, out-of-line template definitions, static data members, etc.

A general answer in LLVM IR or C++ standard parlance is also welcome.

Not sure about clang-query but if clients of your library use existing public headers you can collect declarations by expecting them via libclang . A simple example of this is given in ShlibVisibilityChecker project (it identifies spurious exports from shared libs).

You should be able to get this information through an AST MatchFinder. A simple matcher like

namedDecl().bind("named_decl")

will match all NamedDecl nodes. Then in the callback, you can get the node's Linkage attribute, and process the node accordingly. A callback that printed out which symbols have external linkage might look something like this:

struct LinkagePrinter : public MatchFinder::Callback {
  void run(MatchResult const & result) override {
    using namespace clang;
    NamedDecl const * n_decl =     
      result.Nodes.getNodeAs<NamedDecl("named_decl");
    if(n_decl){
      Linkage l = n_decl->getLinkage();
      switch(l){
        case ExternalLinkage:
          std::cout << "symbol " << n_decl->getNameAsString() 
             << " has external linkage\n";
        // ... etc 
      } 
    } 
    return;
  }
}; // LinkagePrinter

This is roughly right--I haven't checked that this compiles. Register the matcher and callback with a MatchFinder, load the MatchFinder into a Tool, and you should be in business. There are lots of examples in https://github.com/lanl/CoARCT .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM