Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I enforce a compiler warning or error for namespace conflicts?

Consider I have the following:

  • FrameworkA, which defines class Foo
  • FrameworkB, which also defines class Foo
  • A file in FrameworkA which imports FrameworkB

How can I get Xcode to generate either a warning or error on any line that makes references to Foo without using the namespace qualifier?

For example:

let a = FrameworkA.Foo() // fine, no warning or error
let b = FrameworkB.Foo() // fine, no warning or error
let c = Foo()            // at a minimum, a warning

I understand completely that if we are in FrameworkA, then the third example is equivalent to FrameworkA.Foo(), but I would like for Xcode to generate a warning or error.

Consider the scenario when class Foo has existed in FrameworkB for a long time, and the line of code in question has always intended to point at the class Foo defined in FrameworkB, but at some later point in the future, someone added class Foo into FrameworkA for some reason. This would change the behavior of the line in the file.

I would like Xcode to generate compile time warnings or errors any time something defined in multiple frameworks imported into a file is used without the namespace being explicitly declared.

Is there a way?

like image 986
nhgrif Avatar asked Apr 26 '15 14:04

nhgrif


People also ask

How do I enable warnings in GCC?

You can request many specific warnings with options beginning with ' -W ', for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning ' -Wno- ' to turn off warnings; for example, -Wno-implicit .

What is a compile warning?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don't interrupt the compilation process.

How do I disable warning treatment as error?

You can make all warnings being treated as such using -Wno-error. You can make specific warnings being treated as such by using -Wno-error=<warning name> where <warning name> is the name of the warning you don't want treated as an error. If you want to entirely disable all warnings, use -w (not recommended).

How do you use werror?

You can do this by specifying Werror=warning-name , which will cause that specific warning to generate an error. For example, a warning that I promote to an error is -Wreturn-type .


1 Answers

I don't think at this point Xcode supports this unfortunately - some less fruitful solutions:

  • Open a radar task, and hope that Apple fixes it.
  • Prefix your classes (as we used to do with Obj-C)

The second option should be viable for most projects; instead of Foo and Foo, you will have LIBAFoo, LIBBFoo, but in practice, with more meaningful prefixes i.e. CACore Animation.

like image 93
Zorayr Avatar answered Sep 22 '22 08:09

Zorayr