[cfe-dev] questions on the LLVM coding standards

Chris Lattner clattner at apple.com
Tue Nov 16 14:24:22 PST 2010


On Nov 15, 2010, at 5:25 PM, Zhanyong Wan (λx.x x) wrote:
> While reading the LLVM coding standards
> (http://llvm.org/docs/CodingStandards.html), I have some questions:
> 
> 1. It says "you shouldn't assume much about the host compiler,
> including its support for "high tech" features like partial
> specialization of templates."  Is this still the case?  I'm a bit
> surprised that we need to worry about compilers that don't understand
> partial specialization.

Good point, I updated this section.  Partial specialization should still be used sparingly, but just because it can lead to confusing code, not to non-portable code.

> 2. It advises to #include as little as possible.  In particular,
> "Don't do it unless you have to, especially in header files."  And
> later it says "you can include them either directly or indirectly
> (through another header file)."  I'm worried that this may lead
> dependencies on other headers' implementation details.

Yes, this is a possible issue, but since LLVM is a relatively self-contained ecosystem, any issues that arise from this can be addressed by fixing the clients of the headers.

This is in direct contrast to the apparent situation at Google, where they prefer "include what you use".  That approach is taken because it is difficult or expensive to find and update all the clients.

> 3. It says "For example, all of the code in the LLVM project
> implements code that lives in the 'llvm' namespace. As such, it is ok,
> and actually clearer, for the .cpp files to have a 'using namespace
> llvm' directive at their top, after the #includes. The general form of
> this rule is that any .cpp file that implements code in any namespace
> may use that namespace (and its parents'), but should not use any
> others."
> 
> I don't understand why this is necessary.  Since the .cpp is
> implementing code in the llvm namespace, why not enclosing its entire
> body in namespace llvm?  In that case, there's no need for 'using
> namespace llvm'.

It is not "necessary", in that it doesn't affect the semantics of the program.  It is better that putting the entire body of the .cpp file in "namespace llvm {" because it reduces nesting and interoperates better with IDEs that like to indent according to braces.

-Chris





More information about the cfe-dev mailing list