3232
3333import cpp
3434import Deviations
35+ import codingstandards.cpp.Locations
3536
3637string supportedStandard ( ) { result = [ "misra" , "autosar" , "cert" ] }
3738
@@ -226,15 +227,20 @@ class DeviationAttribute extends StdAttribute {
226227
227228 DeviationRecord getADeviationRecord ( ) { result = record }
228229
229- pragma [ nomagic ]
230- Element getASuppressedElement ( ) {
230+ /** Gets the element to which this attribute was applied. */
231+ Element getPrimarySuppressedElement ( ) {
231232 result .( Type ) .getAnAttribute ( ) = this
232233 or
233234 result .( Stmt ) .getAnAttribute ( ) = this
234235 or
235236 result .( Variable ) .getAnAttribute ( ) = this
236237 or
237238 result .( Function ) .getAnAttribute ( ) = this
239+ }
240+
241+ pragma [ nomagic]
242+ Element getASuppressedElement ( ) {
243+ result = this .getPrimarySuppressedElement ( )
238244 or
239245 result .( Expr ) .getEnclosingStmt ( ) = this .getASuppressedElement ( )
240246 or
@@ -289,11 +295,14 @@ newtype TCodeIndentifierDeviation =
289295 } or
290296 TMultiLineDeviation (
291297 DeviationRecord record , DeviationBegin beginComment , DeviationEnd endComment , string filepath ,
292- int suppressedStartLine , int suppressedEndLine
298+ int suppressedStartLine , int suppressedStartColumn , int suppressedEndLine ,
299+ int suppressedEndColumn
293300 ) {
294301 isDeviationRangePaired ( record , beginComment , endComment ) and
295- beginComment .getLocation ( ) .hasLocationInfo ( filepath , suppressedStartLine , _, _, _) and
296- endComment .getLocation ( ) .hasLocationInfo ( filepath , suppressedEndLine , _, _, _)
302+ beginComment
303+ .getLocation ( )
304+ .hasLocationInfo ( filepath , suppressedStartLine , suppressedStartColumn , _, _) and
305+ endComment .getLocation ( ) .hasLocationInfo ( filepath , _, _, suppressedEndLine , suppressedEndColumn )
297306 } or
298307 TCodeIdentifierDeviation ( DeviationRecord record , DeviationAttribute attribute ) {
299308 attribute .getADeviationRecord ( ) = record
@@ -304,7 +313,7 @@ class CodeIdentifierDeviation extends TCodeIndentifierDeviation {
304313 DeviationRecord getADeviationRecord ( ) {
305314 this = TSingleLineDeviation ( result , _, _, _)
306315 or
307- this = TMultiLineDeviation ( result , _, _, _, _, _)
316+ this = TMultiLineDeviation ( result , _, _, _, _, _, _ , _ )
308317 or
309318 this = TCodeIdentifierDeviation ( result , _)
310319 }
@@ -315,18 +324,38 @@ class CodeIdentifierDeviation extends TCodeIndentifierDeviation {
315324 bindingset [ e]
316325 pragma [ inline_late]
317326 predicate isElementMatching ( Element e ) {
318- exists ( string filepath , int elementLocationStart |
319- e .getLocation ( ) .hasLocationInfo ( filepath , elementLocationStart , _, _, _)
327+ exists ( string filepath , int elementLocationStart , int elementLocationColumnStart |
328+ e .getLocation ( )
329+ .hasLocationInfo ( filepath , elementLocationStart , elementLocationColumnStart , _, _)
320330 |
321331 exists ( int suppressedLine |
322332 this = TSingleLineDeviation ( _, _, filepath , suppressedLine ) and
323333 suppressedLine = elementLocationStart
324334 )
325335 or
326- exists ( int suppressedStartLine , int suppressedEndLine |
327- this = TMultiLineDeviation ( _, _, _, filepath , suppressedStartLine , suppressedEndLine ) and
328- suppressedStartLine < elementLocationStart and
329- suppressedEndLine > elementLocationStart
336+ exists (
337+ int suppressedStartLine , int suppressedStartColumn , int suppressedEndLine ,
338+ int suppressedEndColumn
339+ |
340+ this =
341+ TMultiLineDeviation ( _, _, _, filepath , suppressedStartLine , suppressedStartColumn ,
342+ suppressedEndLine , suppressedEndColumn ) and
343+ (
344+ // Element starts on a line after the begin marker of the suppression
345+ suppressedStartLine < elementLocationStart
346+ or
347+ // Element exists on the same line as the begin marker, and occurs after it
348+ suppressedStartLine = elementLocationStart and
349+ suppressedStartColumn < elementLocationColumnStart
350+ ) and
351+ (
352+ // Element starts on a line before the end marker of the suppression
353+ suppressedEndLine > elementLocationStart
354+ or
355+ // Element exists on the same line as the end marker of the suppression, and occurs before it
356+ suppressedEndLine = elementLocationStart and
357+ elementLocationColumnStart < suppressedEndColumn
358+ )
330359 )
331360 )
332361 or
@@ -336,26 +365,64 @@ class CodeIdentifierDeviation extends TCodeIndentifierDeviation {
336365 )
337366 }
338367
368+ /**
369+ * Holds for the region matched by this code identifier deviation.
370+ *
371+ * Note: this is not the location of the marker itself.
372+ */
373+ predicate hasLocationInfo (
374+ string filepath , int suppressedLine , int suppressedColumn , int endline , int endcolumn
375+ ) {
376+ exists ( Comment commentMarker |
377+ this = TSingleLineDeviation ( _, commentMarker , filepath , suppressedLine ) and
378+ suppressedColumn = 1 and
379+ endline = suppressedLine
380+ |
381+ if commentMarker instanceof DeviationEndOfLineMarker
382+ then endcolumn = commentMarker .( DeviationEndOfLineMarker ) .getLocation ( ) .getEndColumn ( )
383+ else
384+ // Find the last column for a location on the next line
385+ endcolumn = getLastColumnNumber ( filepath , suppressedLine )
386+ )
387+ or
388+ this =
389+ TMultiLineDeviation ( _, _, _, filepath , suppressedLine , suppressedColumn , endline , endcolumn )
390+ or
391+ exists ( DeviationAttribute attribute |
392+ this = TCodeIdentifierDeviation ( _, attribute ) and
393+ attribute
394+ .getPrimarySuppressedElement ( )
395+ .getLocation ( )
396+ .hasLocationInfo ( filepath , suppressedLine , suppressedColumn , endline , endcolumn )
397+ )
398+ }
399+
339400 string toString ( ) {
340401 exists ( string filepath |
341402 exists ( int suppressedLine |
342403 this = TSingleLineDeviation ( _, _, filepath , suppressedLine ) and
343404 result =
344- "Deviation record " + getADeviationRecord ( ) + " applied to " + filepath + " Line " +
405+ "Deviation of " + getADeviationRecord ( ) . getQuery ( ) + " applied to " + filepath + " Line " +
345406 suppressedLine
346407 )
347408 or
348- exists ( int suppressedStartLine , int suppressedEndLine |
349- this = TMultiLineDeviation ( _, _, _, filepath , suppressedStartLine , suppressedEndLine ) and
409+ exists (
410+ int suppressedStartLine , int suppressedStartColumn , int suppressedEndLine ,
411+ int suppressedEndColumn
412+ |
413+ this =
414+ TMultiLineDeviation ( _, _, _, filepath , suppressedStartLine , suppressedStartColumn ,
415+ suppressedEndLine , suppressedEndColumn ) and
350416 result =
351- "Deviation record " + getADeviationRecord ( ) + " applied to " + filepath + " Line" +
352- suppressedStartLine + ":" + suppressedEndLine
417+ "Deviation of " + getADeviationRecord ( ) .getQuery ( ) + " applied to " + filepath + " Line " +
418+ suppressedStartLine + ":" + suppressedStartColumn + ":" + suppressedEndLine + ":" +
419+ suppressedEndColumn
353420 )
354421 )
355422 or
356423 exists ( DeviationAttribute attribute |
357424 this = TCodeIdentifierDeviation ( _, attribute ) and
358- result = "Deviation record " + getADeviationRecord ( ) + " applied to " + attribute
425+ result = "Deviation of " + getADeviationRecord ( ) . getQuery ( ) + " applied to " + attribute
359426 )
360427 }
361428}
0 commit comments