What's Flang's preference for `using namespace <name>;`?


I’ve noticed that in many places in the code people tend to use fully qualified names (e.g. mlir::Type or llvm::dyn_cast). Why not add:

  • using namespace mlir;, or
  • using namespace llvm;

at the top of the corresponding source file? Also, Flang has no equivalent of Clang’s LLVM.h or MLIR’s LLVM.h, which are used to “inject” frequently used symbols from other namespaces into the current namespace.

The benefit of fully qualified names is that it is immediately obvious where the symbol comes from (which is very handy in Flang, which depends on 3 large sub-projects: LLVM, MLIR and Clang). However, in practice we end up with the source code being rather noisy, i.e. there are a lot of instances of e.g.:

  • namespace_1::namespace_2::namespace::3::long_type_name).

With using namespace <name>; the code would be less verbose and more consistent with other sub-projects?

I wanted to clarify this as currently Flang mixes both approaches. We still tend to use fully qualified names in many places, but that’s not required with using namespace mlir; present in 2 key header files (in FIROps.h and CGOps.h). And indeed, we tend to mix both approaches in many (most?) files, e.g. in OpenACC.cpp:

That’s inconsistent and can be rather confusing. Are there any preferences? Personally, I would like us to be a bit more liberal about using namespace <name>.

I think that we should begin by removing using namespace mlir; from header files: ⚙ D120897 [flang] Remove 'using namespace mlir;` from header files. Otherwise, it is very hard to tell what namespaces are available in which file (and we end up mixing e.g. Value and mlir::Value).

Thanks for taking a look,

1 Like

Fully qualified names are preferred but of course it’s probably not done everywhere.

1 Like

With using namespace mlir; being present effectively everywhere, it’s not been enforced at all.

Would it makes sense to differentiate per component/directory? For example in the Optimizer it could make sense to have using namespace mlir; but not so much in the rest of the project?