Ignore:
Timestamp:
May 24, 2017, 10:42:09 AM (2 years ago)
Author:
cameron
Message:

Initial support for -q, -l, -L modes, grep options reorganization

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/grep_engine.cpp

    r5464 r5473  
    4444using namespace llvm;
    4545
    46 static cl::OptionCategory bGrepOutputOptions("Output Options",
     46namespace grep {
     47
     48static cl::OptionCategory RE_Options("A. Regular Expression Interpretation", "These options control regular expression parsing and interpretation");
     49
     50re::RE_Syntax RegexpSyntax;
     51static cl::opt<re::RE_Syntax, true> RegexpSyntaxOption(cl::desc("Regular expression syntax: (default PCRE)"),
     52    cl::values(
     53        clEnumValN(re::RE_Syntax::ERE, "E", "Posix extended regular expression (ERE) syntax"),
     54        clEnumValN(re::RE_Syntax::FixedStrings, "F", "Fixed strings, separated by newlines"),
     55        clEnumValN(re::RE_Syntax::BRE, "G", "Posix basic regular expression (BRE) syntax"),
     56        clEnumValN(re::RE_Syntax::PCRE, "P", "Perl-compatible regular expression (PCRE) syntax"),
     57        clEnumValN(re::RE_Syntax::ERE, "extended-regexp", "Alias for -E"),
     58        clEnumValN(re::RE_Syntax::FixedStrings, "fixed-strings", "Alias for -F"),
     59        clEnumValN(re::RE_Syntax::BRE, "basic-regexp", "Alias for -G"),
     60        clEnumValN(re::RE_Syntax::PCRE, "perl-regexp", "Alias for -P"),
     61        clEnumValN(re::RE_Syntax::PROSITE, "PROSITE", "PROSITE protein patterns syntax"),
     62        clEnumValEnd), cl::cat(RE_Options), cl::Grouping, cl::location(RegexpSyntax), cl::init(re::RE_Syntax::PCRE));
     63
     64bool IgnoreCaseFlag;
     65static cl::opt<bool, true> IgnoreCase("i", cl::desc("Ignore case distinctions in the pattern and the file (alias: -ignore-case)."),
     66                                      cl::cat(RE_Options), cl::location(IgnoreCaseFlag), cl::Grouping);
     67static cl::alias IgnoreCaseAlias("ignore-case", cl::desc("Alias for -i"), cl::aliasopt(IgnoreCase), cl::NotHidden);
     68
     69bool InvertMatchFlag;
     70static cl::opt<bool, true> InvertMatch("v", cl::desc("Invert match results: select non-matching lines (alias: -invert-match)."),
     71                                       cl::cat(RE_Options), cl::location(InvertMatchFlag), cl::Grouping);
     72static cl::alias InvertMatchAlias("invert-match", cl::desc("Alias for -v"), cl::aliasopt(InvertMatch), cl::NotHidden);
     73
     74bool LineRegexpFlag;
     75static cl::opt<bool, true> LineRegexp("x", cl::desc("Require that entire lines be matched (alias: -line-regexp)."), cl::cat(RE_Options),
     76                                      cl::location(LineRegexpFlag), cl::Grouping);
     77static cl::alias LineRegexpAlias("line-regexp", cl::desc("Alias for -x"), cl::aliasopt(LineRegexp), cl::NotHidden);
     78
     79bool WordRegexpFlag;
     80static cl::opt<bool, true> WordRegexp("w", cl::desc("Require that that whole words be matched (alias: -word-regexp)."), cl::cat(RE_Options),
     81                                      cl::location(WordRegexpFlag), cl::Grouping);
     82static cl::alias WordRegexpAlias("word-regexp", cl::desc("Alias for -w"), cl::aliasopt(WordRegexp), cl::NotHidden);
     83
     84const cl::OptionCategory * grep_regexp_flags() {
     85    return &RE_Options;
     86}
     87
     88static cl::OptionCategory GrepInputOptions("B. Input Options",
     89                                             "These options control the input.");
     90
     91static cl::opt<bool> NullData("z", cl::desc("Use the NUL character (codepoint 00) as the line-break character for input."), cl::cat(GrepInputOptions), cl::Grouping);
     92static cl::alias NullDataAlias("null-data", cl::desc("Alias for -z"), cl::aliasopt(NullData));
     93
     94bool RecursiveFlag;
     95static cl::opt<bool, true> Recursive("r", cl::desc("Recursively process files within directories, (but follow only top-level symlinks unless -R)."),
     96                               cl::location(RecursiveFlag), cl::cat(GrepInputOptions), cl::Grouping);
     97static cl::alias RecursiveAlias("recursive", cl::desc("Alias for -r"), cl::aliasopt(Recursive));
     98
     99bool DereferenceRecursiveFlag;
     100static cl::opt<bool, true> DereferenceRecursive("R", cl::desc("Recursively process files within directories, following symlinks at all levels."),
     101                                          cl::location(DereferenceRecursiveFlag), cl::cat(GrepInputOptions), cl::Grouping);
     102static cl::alias DereferenceRecursiveAlias("dereference-recursive", cl::desc("Alias for -R"), cl::aliasopt(DereferenceRecursive));
     103
     104
     105
     106
     107static cl::OptionCategory bGrepOutputOptions("C. Output Options",
    47108                                             "These options control the output.");
     109
     110GrepModeType Mode;
     111static cl::opt<GrepModeType, true> GrepModeOption(cl::desc("Abbreviated output mode options:"),
     112    cl::values(
     113        clEnumValN(CountOnly, "c", "Display only the count of matching lines per file."),
     114        clEnumValN(FilesWithMatch, "l", "Display only the names of files that have at least one match to the pattern."),
     115        clEnumValN(FilesWithoutMatch, "L", "Display only the names of files that do not match the pattern."),
     116        clEnumValN(QuietMode, "q", "Do not generate any output and ignore errors; set the return to zero status if a match is found."),
     117        clEnumValN(CountOnly, "count", "Alias for -c"),
     118        clEnumValN(FilesWithMatch, "files-with-match", "Alias for -l"),
     119        clEnumValN(FilesWithoutMatch, "files-without-match", "Alias for -L"),
     120        clEnumValN(QuietMode, "quiet", "Alias for -q"),
     121        clEnumValN(QuietMode, "silent", "Alias for -q"),
     122        clEnumValEnd), cl::cat(bGrepOutputOptions), cl::Grouping, cl::location(Mode), cl::init(NormalMode));
     123
     124
    48125static cl::opt<bool> SilenceFileErrors("s", cl::desc("Suppress messages for file errors."), cl::init(false),  cl::cat(bGrepOutputOptions));
    49 
    50 static cl::opt<bool> SuppressOutput("q", cl::desc("Suppress normal output; set return code only."), cl::init(false),  cl::cat(bGrepOutputOptions));
    51126
    52127static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
     
    58133static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
    59134
    60 static cl::opt<int> MaxCount("m", cl::desc("Limit the number of matches per file."), cl::cat(bGrepOutputOptions), cl::init((size_t) -1));
     135static cl::opt<int> MaxCount("m", cl::desc("Limit the number of matches per file."), cl::cat(bGrepOutputOptions), cl::init((size_t) 0), cl::Prefix);
    61136static cl::alias MaxCountLong("max-count", cl::desc("Alias for -m"), cl::aliasopt(MaxCount));
     137
     138static cl::opt<int> AfterContext("A", cl::desc("Print <num> lines of context after each matching line."), cl::cat(bGrepOutputOptions), cl::Prefix);
     139static cl::alias AfterContextAlias("after-context", cl::desc("Alias for -A"), cl::aliasopt(AfterContext));
     140
     141static cl::opt<int> BeforeContext("B", cl::desc("Print <num>lines of context before each matching line."), cl::cat(bGrepOutputOptions), cl::Prefix);
     142static cl::alias BeforeContextAlias("before-context", cl::desc("Alias for -B"), cl::aliasopt(BeforeContext));
     143
     144static cl::opt<int> Context("C", cl::desc("Print <num> lines of context before and after each matching line."), cl::cat(bGrepOutputOptions), cl::Prefix);
     145static cl::alias ContextAlias("context", cl::desc("Alias for -C"), cl::aliasopt(Context));
     146
     147static cl::opt<bool> OnlyMatching("o", cl::desc("Display only the exact strings that match the pattern, with possibly multiple matches per line."), cl::cat(bGrepOutputOptions), cl::Grouping);
     148static cl::alias OnlyMatchingAlias("only-matching", cl::desc("Alias for -o"), cl::aliasopt(OnlyMatching));
     149
     150static cl::opt<bool> Null("Z", cl::desc("Write NUL characters after filenames generated to output."), cl::cat(bGrepOutputOptions), cl::Grouping);
     151static cl::alias NullAlias("null", cl::desc("Alias for -Z"), cl::aliasopt(Null));
     152
     153static cl::opt<bool> ByteOffset("b", cl::desc("Show the byte offset within the file for each matching line."), cl::cat(bGrepOutputOptions), cl::Grouping);
     154static cl::alias ByteOffsetAlias("byte-offset", cl::desc("Alias for -b"), cl::aliasopt(ByteOffset));
     155
     156static cl::opt<bool> UnixByteOffsets("u", cl::desc("If byte offsets are displayed, report offsets as if all lines are terminated with a single LF."), cl::cat(bGrepOutputOptions), cl::Grouping);
     157static cl::alias UnixByteOffsetsAlias("unix-byte-offsets", cl::desc("Alias for -u"), cl::aliasopt(UnixByteOffsets));
     158
     159static cl::opt<bool> InitialTab("T", cl::desc("Line up matched line content using an inital tab character."), cl::cat(bGrepOutputOptions), cl::Grouping);
     160static cl::alias InitialTabAlias("initial-tab", cl::desc("Alias for -T"), cl::aliasopt(InitialTab));
     161
     162
     163const cl::OptionCategory * grep_output_flags() {
     164    return &bGrepOutputOptions;
     165}
     166
     167const cl::OptionCategory * grep_input_flags() {
     168    return &GrepInputOptions;
     169}
     170
    62171
    63172static re::CC * parsedCodePointSet = nullptr;
     
    68177size_t * startPoints = nullptr;
    69178size_t * accumBytes = nullptr;
     179
    70180
    71181void GrepEngine::doGrep(const std::string & fileName) const{
     
    211321}
    212322
    213 void PrintResult(bool CountOnly, std::vector<size_t> & total_CountOnly){
    214     if (CountOnly) {
     323const int MatchFoundReturnCode = 0;
     324const int MatchNotFoundReturnCode = 1;
     325void PrintResult(GrepModeType grepMode, std::vector<size_t> & total_CountOnly){
     326    if (grepMode == NormalMode) {
     327        int returnCode = MatchNotFoundReturnCode;
     328        for (unsigned i = 0; i < inputFiles.size(); ++i){
     329            std::cout << resultStrs[i].str();
     330            if (!resultStrs[i].str().empty()) returnCode = MatchFoundReturnCode;
     331        }
     332        exit(returnCode);
     333    }
     334    if (grepMode == CountOnly) {
     335        size_t total = 0;
    215336        if (!ShowFileNames) {
    216             for (unsigned i = 0; i < inputFiles.size(); ++i){
     337            for (unsigned i = 0; i < inputFiles.size(); ++i) {
    217338                std::cout << total_CountOnly[i] << std::endl;
     339                total += total_CountOnly[i];
    218340            }
    219341        } else {
    220342            for (unsigned i = 0; i < inputFiles.size(); ++i){
    221343                std::cout << inputFiles[i] << ':' << total_CountOnly[i] << std::endl;
     344                total += total_CountOnly[i];
    222345            };
    223346        }
    224     } else {
     347        exit(total == 0 ? MatchNotFoundReturnCode : MatchFoundReturnCode);
     348    }
     349    else if (grepMode == FilesWithMatch || grepMode == FilesWithoutMatch ) {
     350        size_t total = 0;
     351        size_t requiredCount = grepMode == FilesWithMatch ? 1 : 0;
     352        for (unsigned i = 0; i < inputFiles.size(); ++i) {
     353            if (total_CountOnly[i] == requiredCount) {
     354                std::cout << inputFiles[i] << std::endl;
     355            }
     356            total += total_CountOnly[i];
     357        }
     358        exit(total == 0 ? MatchNotFoundReturnCode : MatchFoundReturnCode);
     359    } else /* QuietMode */ {
    225360        for (unsigned i = 0; i < inputFiles.size(); ++i){
    226             std::cout << resultStrs[i].str();
    227         }
     361            if (total_CountOnly[i] > 0) exit(MatchFoundReturnCode);
     362        }
     363        exit(MatchNotFoundReturnCode);
    228364    }
    229365}
     
    253389}
    254390
    255 void GrepEngine::grepCodeGen_nvptx(std::vector<re::RE *> REs, const bool CountOnly, const bool UTF_16) {
     391void GrepEngine::grepCodeGen_nvptx(std::vector<re::RE *> REs, const GrepModeType grepMode, const bool UTF_16) {
    256392
    257393    NVPTXDriver pxDriver("engine");
     
    342478}
    343479
    344 void GrepEngine::grepCodeGen(std::vector<re::RE *> REs, const bool CountOnly, const bool UTF_16, GrepSource grepSource, const GrepType grepType) {
     480void GrepEngine::grepCodeGen(std::vector<re::RE *> REs, const GrepModeType grepMode, const bool UTF_16, GrepSource grepSource, const GrepType grepType) {
    345481
    346482    ParabixDriver pxDriver("engine");
     
    359495    StreamSetBuffer * ByteStream = nullptr;
    360496    kernel::Kernel * sourceK = nullptr;
     497   
     498    size_t MatchLimit = ((grepMode == QuietMode) | (grepMode == FilesWithMatch) | (grepMode == FilesWithoutMatch)) ? 1 : MaxCount;
    361499
    362500    if (grepSource == GrepSource::Internal) {
     
    432570    }
    433571   
    434     if (AlgorithmOptionIsSet(re::InvertMatches)) {
     572    if (InvertMatch) {
    435573        kernel::Kernel * invertK = pxDriver.addKernelInstance(make_unique<kernel::InvertMatchesKernel>(idb));
    436574        StreamSetBuffer * OriginalMatches = MergedResults;
     
    438576        pxDriver.makeKernelCall(invertK, {OriginalMatches, LineBreakStream}, {MergedResults});
    439577    }
    440     if (MaxCount > 0) {
     578    if (MatchLimit > 0) {
    441579        kernel::Kernel * untilK = pxDriver.addKernelInstance(make_unique<kernel::UntilNkernel>(idb));
    442         untilK->setInitialArguments({idb->getSize(MaxCount)});
     580        untilK->setInitialArguments({idb->getSize(MatchLimit)});
    443581        StreamSetBuffer * AllMatches = MergedResults;
    444582        MergedResults = pxDriver.addBuffer(make_unique<CircularBuffer>(idb, idb->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    445583        pxDriver.makeKernelCall(untilK, {AllMatches}, {MergedResults});
    446584    }
    447     if (CountOnly) {
     585    if (grepMode != NormalMode) {
    448586        kernel::MatchCount matchCountK(idb);
    449587        pxDriver.addKernelCall(matchCountK, {MergedResults}, {});
     
    510648
    511649}
     650}
Note: See TracChangeset for help on using the changeset viewer.