Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage of inline functions using F# in visual studio 2012

The code coverage tool in Visual studio 2012 marks all inline functions as uncovered even when they were covered.

Is there any way how to make code coverage work for inline functions?

like image 895
Oldrich Svec Avatar asked Mar 01 '13 20:03

Oldrich Svec


People also ask

What is inline F?

f = inline( expr , arg1,arg2,…,argN ) constructs an inline function whose input arguments are specified by arg1,arg2,…,argN . Multicharacter symbol names may be used. f = inline( expr , N ) , where N is a scalar, constructs an inline function whose input arguments are x and P1,P2,…,PN .

What is inline function example?

Example. In the following class declaration, the Account constructor is an inline function. The member functions GetBalance , Deposit , and Withdraw aren't specified as inline but can be implemented as inline functions. In the class declaration, the functions were declared without the inline keyword.

Which keyword is used for inline function?

Inline functions are expanded by the compiler. Macros are expanded by the preprocessor. They are defined by the keyword inline. They are defined by the keyword #define.

What is inline function in C# with example?

An inline C# function is a CMS function written in C# and normally created in the CMS Console (unlike external C# functions, created in external editors and added to C1 CMS). To create an inline C# function: In Functions, select C# Functions and click Add InlineC# Function.


2 Answers

When you write an inline function, the compiler inlines the body of the function into the calling code. As discussed in the comments, it also does not emit debug information for this inlined body (and so you cannot step into the function - I think this could be fixed - submit a request to fsbugs at microsoft dot com).

However, the compiler also generates a dynamic version of the code - if you write say:

let inline add a b = a + b

then there is actually an add method in the compiled code. The method will be generic ('a -> 'b -> 'c) and uses dynamic lookup to find an appropriate implementation of the + operator (because .NET generics cannot represent such constraints).

The dynamic implementation will be invoked when you use reflection or quotations:

open Microsoft.FSharp.Linq.RuntimeHelpers
<@ add 1 2 @> |> LeafExpressionConverter.EvaluateQuotation |> printfn "Got: %A"

If you put breakpoint inside add and run the above code than the breakpoint will be hit (and I assume the code coverage tool will report the function as covered).

But keep in mind that this is not the code that is actually executed when you write add 1 2 in normal code - the code executing here is a different implementation (that uses slow dynamic lookup, rather than inlining).

like image 80
Tomas Petricek Avatar answered Nov 06 '22 23:11

Tomas Petricek


Aren't inline functions directly integrated into the calling code? If so no actual method is called as the code is 'inlined' and for each place they are used a new copy of the inlined code exists.

like image 43
Shaun Wilde Avatar answered Nov 06 '22 22:11

Shaun Wilde