Exemplo n.º 1
0
  private void leave_file() {
    SVPreProc2InputData old_file = fInputCurr;

    // Clean up (if needed) after the macro stack
    // Only cleanup after leaving a file where file content (not expanded macro content)
    // was present
    if (fInputCurr.incPos()) {
      cleanup_preproc_leftovers();
    }

    fInputCurr.leave_file();

    if (fIndexStats != null) {
      // Update stats
      if (old_file.getInput() instanceof SVFileBuffer) {
        fIndexStats.incLastIndexFileReadTime(((SVFileBuffer) old_file.getInput()).getReadTime());
      }
      fIndexStats.incNumLines(old_file.getLineCount());
      fIndexStats.incNumProcessedFiles();
    }

    /*SVPreProc2InputData curr_in =*/ fInputStack.pop();

    SVPreProc2InputData new_file = fInputStack.peek();
    fInputCurr = new_file;

    int file_idx = fFileList.indexOf(new_file.getFileName());

    // We will not have registered ANONYMOUS files
    if (file_idx != -1) {
      int file_id = 0;

      if (fFileMapper != null) {
        file_id = fFileMapper.mapFilePathToId(new_file.getFileName(), true);
      }

      // Add a marker noting that we're switching to a new file
      SVPreProcOutput.FileChangeInfo file_info =
          new SVPreProcOutput.FileChangeInfo(fOutputLen, file_id, new_file.getLineNo());
      fFileMap.add(file_info);
    }
  }
Exemplo n.º 2
0
  public SVPreProcOutput preprocess() {
    int ch, last_ch = -1;
    int end_comment1 = -1, end_comment2 = -1;
    long start, end;
    boolean in_string = false;
    boolean ifdef_enabled = true;
    boolean found_single_line_comment = false;

    start = System.currentTimeMillis();

    while ((ch = get_ch()) != -1) {
      found_single_line_comment = false;
      if (!in_string) {
        // Handle comment
        if (ch == '/') {
          int ch2 = get_ch();

          if (ch2 == '/') {
            output(' ');
            found_single_line_comment = true;
            beginComment();
            while ((ch = get_ch()) != -1 && ch != '\n' && ch != '\r') {
              fCommentBuffer.append((char) ch);
            }
            fCommentBuffer.append('\n');

            // Handle
            if (ch == '\r') {
              ch = get_ch();
              if (ch != '\n') {
                unget_ch(ch);
              }
            }
            ch = '\n';
            last_ch = ' ';
          } else if (ch2 == '*') {
            end_comment1 = -1;
            end_comment2 = -1;

            output(' ');

            beginComment();
            while ((ch = get_ch()) != -1) {
              end_comment1 = end_comment2;
              end_comment2 = ch;

              if (end_comment1 == '*' && end_comment2 == '/') {
                endComment();
                break;
              } else {
                fCommentBuffer.append((char) ch);
              }
            }
            ch = ' ';
            last_ch = ' ';
          } else {
            unget_ch(ch2);
          }
        }

        if (!Character.isWhitespace(ch) && fInComment) {
          // Send accumlated comment to observer
          endComment();
        }

        if (ch == '`') {
          // Processing an ifdef may affect enablement
          handle_preproc_directive();
          ifdef_enabled = ifdef_enabled();
          if (!ifdef_enabled) {
            output(' ');
          }
        } else {
          if (ch == '"' && last_ch != '\\') {
            // Enter string
            in_string = true;
          }
          if (ifdef_enabled) {
            output((char) ch);
          }
        }
      } else {
        // In String
        if (ch == '"' && last_ch != '\\') {
          in_string = false;
        }
        if (ifdef_enabled) {
          output((char) ch);
        }
      }

      // Consecutive back-slashes convert to
      // a single backslash. For tracking purposes,
      // convert to space
      if (last_ch == '\\' && ch == '\\') {
        last_ch = ' ';
      } else {
        last_ch = ch;
      }

      if (fInComment && !found_single_line_comment && ch == '\n') {
        endComment();
      }
    }

    SVPreProcOutput ret = new SVPreProcOutput(fOutput, null, fFileMap, fFileList);
    ret.setFileTree(fInputCurr.getFileTree());

    // Clean up after any unbalanced pre-processor directives
    cleanup_preproc_leftovers();

    // Leave final file
    fInputCurr.close();

    // Finally, save the full pre-processor state to the final file
    /*
    last_file.fFileTree.fDefinedMacros.clear();
    for (Entry<String, SVDBMacroDef> e : fMacroMap.entrySet()) {
    	if (!e.getKey().equals("__FILE__") && !e.getKey().equals("__LINE__")) {
    		last_file.fFileTree.fDefinedMacros.put(e.getKey(), e.getValue());
    	}
    }
     */

    end = System.currentTimeMillis();

    if (fIndexStats != null) {
      if (fInputCurr.getInput() instanceof SVFileBuffer) {
        fIndexStats.incLastIndexFileReadTime(((SVFileBuffer) fInputCurr.getInput()).getReadTime());
      }
      fIndexStats.incNumLines(fInputCurr.getLineCount());
      fIndexStats.incLastIndexPreProcessTime(end - start);
      fIndexStats.incNumProcessedFiles();
    }

    return ret;
  }