Custom Compiler Warnings

Custom Compiler Warnings

Update

This is now possible with Roslyn (Visual Studio 2015). You can build a code analyzer to check for a custom attribute


Original outdated answer:

I don't believe it's possible. ObsoleteAttribute is treated specially by the compiler and is defined in the C# standard. Why on earth is ObsoleteAttribute not acceptable? It seems to me like this is precisely the situation it was designed for, and achieves precisely what you require!

Also note that Visual Studio picks up the warnings generated by ObsoleteAttribute on the fly too, which is very useful.

Don't mean to be unhelpful, just wondering why you're not keen on using it...

Unfortunately ObsoleteAttribute is sealed (probably partly due to the special treatment) hence you can't subclass your own attribute from it.

From the C# standard:-

The attribute Obsolete is used to mark
types and members of types that should
no longer be used.

If a program uses a type or member
that is decorated with the Obsolete
attribute, the compiler issues a
warning or an error. Specifically, the
compiler issues a warning if no error
parameter is provided, or if the error
parameter is provided and has the
value false. The compiler issues an
error if the error parameter is
specified and has the value true.

Doesn't that sum up your needs?... you're not going to do better than that I don't think.

How can I create custom compiler warnings in java?

While @Christian Hujer did provide a solid solution, I chose to go another route which has been working out well.

There is a wrapper class "Resource" around the SynchronizedCollection which contains:

  • A semaphore for locking the collection
  • A randomly generated ID representing the key to the currently held lock
  • Methods for performing atomic operations on the collection (They acquire the lock, perform the operation, and immediately release it)
  • Methods for performing non-atomic operations on the collection (They accept an ID as the key and perform the requested operation if the provided key matches the key currently holding the lock)

The class described above is enough to provide sufficient protection around the collection, but what I wanted was compiler warnings if the lock wasn't released.

To accomplish this, there is a "ResourceManager" which implements java.lang.AutoCloseable

This class:

  • Is passed a reference to the "Resource" via the constructor
  • Acquires the lock for the reference in the constructor
  • Provides an API for calling the non-atomic methods on the "Resource" using the key it acquired during construction
  • Provides a close() method, overriding java.lang.AutoCloseable, which releases the lock acquired during construction

The resource manager is created wherever multiple operations need to be performed on the Resource and a compiler warning is generated if close() is not called on any particular code path. Additionally, in java 7+, the manager can be created in a try-with-resource block and the lock is automatically released, regardless of what happens in the block.

Add my own compiler warning

In Visual Studio,

#pragma message ("Warning goes here")

On a side note, if you want to suppress such warnings, find the compiler warning ID (for the deprecated warning, it's C4996) and insert this line:

#pragma warning( disable : 4996)

Generating a custom compile time warning C#

You can do that with PostSharp.

I've once done it, and explained how to do it here

Add custom compiler warning to virtual method

No, there is nothing as specific as that. I think your options are:

  1. Write a custom analyzer, as suggested by Panagiotis Kanavos. That analyzer could either be specific to your type, or it could detect the presence of an attribute, as you suggested.

    I think that the resulting analyzer would not be very complex, but learning how to write it could take some time, if you've never used the Roslyn API.

  2. Figure out a different coding pattern that achieves a similar purpose. For example, you could have one type where both methods are abstract (so you have to override both) and one where they are sealed (so you can't override them).

    I'm not sure if this specific approach would be worth it, but maybe something similar is worth considering.

OCaml custom compiler warnings

A common trick is to raise an exception with a failwith "not implemented" or just assert false.

This will give you a warning if you have unused parameters in the unimplemented function. Although, they are disable by default. So you need to add -Wall option, to enable them.

Also, you can just add an intentionally unused variable in your implementation, like:

let reverse xs =
let unimplemented = () in
xs


Related Topics



Leave a reply



Submit