atulya
atulya

Reputation: 539

How visual studio intellisense works for compiler errors

I am curious to know how, visual studio (2010 onward) intellisense works on compiler errors.

I mean to say that, for example

  1. when you declare and initialize any variable in a function, immediately a zig-zag line comes under a variable name and when you hover the mouse over it, it says, "variable is initialized but not used".

or

  1. A function needs to return some value and you don't write any return statement in the function, immediately a zig-zag lines comes under function name, it says, "all paths does not returns".

How this intelligence is implemented?

How I can add my own rules, if I want to?

Any guidance/link will help?

Upvotes: 1

Views: 563

Answers (1)

jessehouwing
jessehouwing

Reputation: 114822

Visual Studio employs different ways to surface these kinds of warnings and it depends on the language you're editing (C# vs C++ vs VB.NET etc) what technology is employed.

  1. Syntax validation. Your source code has a syntax defined. If you open a {, the syntax definition knows that there must be a } somewhere further along in the file, and thus these simple checks are done purely on the source code structure.
  2. Flow validation. You mentioned the return statements and null value issues and these are a bit more tricky. Once the source file is structurally correct it's possible to do simple flow analysis on the AST and ensure that every branch in the structure will result in a return statement, or that a variable has a value assigned before it's being used.

Then there is a more advanced type of validation that can take place, one that actually takes in the context of all the parsed files and/or their binary representation. VB.NET for example employs

  1. Background compilation. This is a process that will actually try to do a compile step as soon as syntax and flow validation succeed. It runs in the background and will incrementally try to update the compiled product. if it encounters issues, it will flag them as soon as possible. Tools like Red-Gate .NET Demon add similar features to other languages in Visual Studio. Whether the compiler actually writes anything to disk, or just updates an in memory representation of the compiled product doesn't really matter as far as Intellisense/Editor is concerned.

And then there are tools like Resharper and Code Rush which build up an in memory representation of the code. Visual Studio also builds up a similar model, though not as feature rich as these IDE productivity tools in many cases

  1. Advanced code model. Not as complete or as direct as background compilation (sometimes the code may actually compile, but a bug in the state model might show red squiggles from these tools), these tools build an in memory model of the code and can employ very advanced flow validation on your code to flag all kinds of issues. Both Resharper and Code Rush provide an SDK to extend these validations. The Visual Studio compiler and editor will at some point be replaced with Project Roslyn, which offers a similar SDK.

And then there's the information that's added during build

  1. MsBuild. When you trigger a build a large list of custom tools can trigger. StyleCop and code Analysis for example offer tools to do language or binary analysis on the code being compiled. And both can be extended with additional rules. MsBuild will surface errors and warnings in the editor and if these provide a file name, and line number, the editor will happily put a squiggly in the text rendering.

Intellisense uses the data provides by the Visual Studio Language Service and by VSPackages and MsBuild to render the text in the editor. As you can see there are multiple ways in which such a result is gathered. The Visual Studio editor provides a number of extensibility points which you can use to add your own smart tags and adornments to the text.

Plus you have a number of 3rd party tools that provide similar functionality.

A great source for insights in how these things work, are the ASP.NET MVC editors for Visual studio or Python plugins for Visual Studio.

Upvotes: 1

Related Questions