#318 Spurious "Unbalanced pre-processor directed" when no newline after final `endif

v1.0_(example)
closed-fixed
nobody
None
5
2016-06-18
2014-02-17
Armond
No

The standard multiple inclusion guard gets marked with an "Unbalanced pre-preprocessor directive" error if there is no newline after the final '`endif'

`ifndef __MY_FILE_SV__
`define __MY_FILE_SV__
interface my_interface_if ;
  logic thingy ;
endinterface
`endif    // <-- If there is NO newline here.... 
          //     last char is the 'f'

Discussion

  • Armond

    Armond - 2014-02-17
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,12 +1,11 @@
    -
     The standard multiple inclusion guard gets marked with an "Unbalanced pre-preprocessor directive" error if there is no newline after the final '`endif'
    
    -`ifndef __MY_FILE_SV__
    -`define __MY_FILE_SV__
    -interface my_interface_if ;
    -  logic thingy ;
    -endinterface
    -`endif    // <-- If there is NO newline here.... 
    -          //     last char is the 'f'
    +    `ifndef __MY_FILE_SV__
    +    `define __MY_FILE_SV__
    +    interface my_interface_if ;
    +      logic thingy ;
    +    endinterface
    +    `endif    // <-- If there is NO newline here.... 
    +              //     last char is the 'f'
    
     
  • StevenAZ

    StevenAZ - 2016-04-07

    Just tested this in 1.8.3. I don't see the issue. Can I close it as fixed?

     
  • StevenAZ

    StevenAZ - 2016-04-13

    6 days after stating that I can't replicate the issue, I replicate the issue. Looks like it stays open for now.

     
  • StevenAZ

    StevenAZ - 2016-04-13

    The trick is to have the code above as a file that is included in another file. Below I have 2 files, which replicate the issue. Looks like the "fileid" of the `endif is being added incorrectly (i.e. bleeds over into the next file).

    top.sv:

    `include "sub.sv"
    

    sub.sv:

    `ifdef bob
    `endif
    
     
    Last edit: StevenAZ 2016-04-13
  • StevenAZ

    StevenAZ - 2016-04-14

    Poking around a bit ... it appears that the following occurs in SVPreProcessor2.java:

    SVPreProcessor2.java will call "AbstractTextScanner:readPreProcIdentifier" which calls "get_ch". If "get_ch" reaches an end of file, it calls "leave_file" before returning, which in turn will call the "cleanup_preproc_leftovers" which flags an error.

    Once we return, "readPreProcIdentifier" will retun the "endif" which would normally pop the identifier stack, but too late.

    There's probably a million ways of doing this ...

    One crude way is to replace the "AbstractTextScanner.java::readPreProcIdentifier" with the following code. Down side is that we are doing a string compare every time we read a character in a `some_field which could slow the parsing down.

    I dare say there is a more elegant, less expensive way of fixing this.

    ~~~~~~~

    public String readPreProcIdentifier(int ci) {
        fTmpBuffer.setLength(0);
    
        if (!SVCharacter.isSVIdentifierStart(ci)) {
            unget_ch(ci);
            return null;
        }
    
        fTmpBuffer.append((char)ci);
    
        while ((ci = get_ch()) != -1 && SVCharacter.isSVIdentifierPart(ci)) {
            fTmpBuffer.append((char)ci);
            // Patch for reaching end of file while reading "endif"
            // get_ch will call "leave_file" which checks for unballanced preprocessor 
            // directives, marking the `ifdef ahead of this as unballanced
            // This hack will head back as soon as the endif is read... allowing the endif
            // to be matched
            if (fTmpBuffer.toString().equals("endif"))  {
                return ("endif");
            }
        }
        unget_ch(ci);
    
        return (fTmpBuffer.length()>0)?fTmpBuffer.toString():null;
    }
    
     
    Last edit: StevenAZ 2016-04-14
  • Matthew Ballance

    • status: open --> closed-fixed
     
  • Matthew Ballance

    Corrected in 1.8.8. Updated the preprocessor to ensure that every file appears to end with a newline.

     

Log in to post a comment.