001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.cnd.modelimpl.parser.apt;
029:
030: import java.util.ArrayList;
031: import java.util.List;
032: import org.netbeans.modules.cnd.api.model.CsmFile;
033: import org.netbeans.modules.cnd.api.model.CsmOffsetable;
034: import org.netbeans.modules.cnd.apt.structure.APT;
035: import org.netbeans.modules.cnd.apt.structure.APTFile;
036: import org.netbeans.modules.cnd.apt.support.APTPreprocHandler;
037: import org.netbeans.modules.cnd.modelimpl.csm.core.Utils;
038:
039: /**
040: * This walker gathers information about code blocks being hidden by preprocessor
041: * commands
042: *
043: * @author Sergey Grinev
044: */
045: public class APTFindUnusedBlocksWalker extends APTSelfWalker {
046:
047: private final List<CsmOffsetable> blocks = new ArrayList<CsmOffsetable>();
048:
049: public List<CsmOffsetable> getBlocks() {
050: return blocks;
051: }
052:
053: protected void addBlock(int startOffset, int endOffset) {
054: blocks.add(Utils.createOffsetable(csmFile, startOffset,
055: endOffset));
056: }
057:
058: public APTFindUnusedBlocksWalker(APTFile apt, CsmFile csmFile,
059: APTPreprocHandler preprocHandler) {
060: super (apt, csmFile, preprocHandler);
061: }
062:
063: protected @Override
064: boolean onIfdef(APT apt) {
065: boolean val = super .onIfdef(apt);
066: handleIf(apt, val);
067: return val;
068: }
069:
070: protected @Override
071: boolean onIfndef(APT apt) {
072: boolean val = super .onIfndef(apt);
073: handleIf(apt, val);
074: return val;
075: }
076:
077: protected @Override
078: boolean onIf(APT apt) {
079: boolean val = super .onIf(apt);
080: handleIf(apt, val);
081: return val;
082: }
083:
084: protected @Override
085: boolean onElif(APT apt, boolean wasInPrevBranch) {
086: boolean val = super .onElif(apt, wasInPrevBranch);
087: handleIf(apt, val);
088: return val;
089: }
090:
091: protected @Override
092: boolean onElse(APT apt, boolean wasInPrevBranch) {
093: boolean val = super .onElse(apt, wasInPrevBranch);
094: handleIf(apt, val);
095: return val;
096: }
097:
098: private void handleIf(APT opener, boolean value) {
099: APT closer = opener.getNextSibling();
100: if (closer != null && !value) {
101: addBlock(opener.getEndOffset(), closer.getOffset() - 1);
102: }
103: }
104: }
|