Doxygen
Loading...
Searching...
No Matches
definition.h File Reference
#include <vector>
#include "construct.h"
#include "reflist.h"
#include "requirement.h"
#include "types.h"
Include dependency graph for definition.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  DocInfo
 Data associated with a detailed description. More...
struct  BriefInfo
 Data associated with a brief description. More...
struct  BodyInfo
 Data associated with description found in the body. More...
class  Definition
 The common base class of all entity definitions found in the sources. More...
class  DefinitionMutable

Functions

DefinitiontoDefinition (DefinitionMutable *dm)
DefinitionMutabletoDefinitionMutable (Definition *d)
bool readCodeFragment (const DString &fileName, bool isMacro, int &startLine, int &endLine, DString &result)
 Reads a fragment from file fileName starting with line startLine and ending with line endLine.

Function Documentation

◆ readCodeFragment()

bool readCodeFragment ( const DString & fileName,
bool isMacro,
int & startLine,
int & endLine,
DString & result )

Reads a fragment from file fileName starting with line startLine and ending with line endLine.

The result is returned as a string via result. The function returns true if successful and false in case of an error.

Reads a fragment of code from file fileName starting at line startLine and ending at line endLine (inclusive). The fragment is stored in result. If false is returned the code fragment could not be found.

The file is scanned for a opening bracket ('{') from startLine onward The line actually containing the bracket is returned via startLine. The file is scanned for a closing bracket ('}') from endLine backward. The line actually containing the bracket is returned via endLine. Note that for VHDL code the bracket search is not done.

Definition at line 760 of file definition.cpp.

762{
763 bool filterSourceFiles = Config_getBool(FILTER_SOURCE_FILES);
764 DString filter = getFileFilter(fileName,true);
765 bool usePipe = !filter.empty() && filterSourceFiles;
766 int tabSize = Config_getInt(TAB_SIZE);
767 SrcLangExt lang = getLanguageFromFileName(fileName);
768 const int blockSize = 4096;
769 std::string str;
771 static_cast<size_t>(std::max(1,startLine)),
772 static_cast<size_t>(std::max({1,startLine,endLine})),str);
773 //printf("readCodeFragment(%s,startLine=%d,endLine=%d)=\n[[[\n%s]]]\n",qPrint(fileName),startLine,endLine,qPrint(str));
774
775 bool found = lang==SrcLangExt::VHDL ||
776 lang==SrcLangExt::Python ||
777 lang==SrcLangExt::Fortran ||
778 isMacro;
779 // for VHDL, Python, and Fortran no bracket search is possible
780 char *p=str.data();
781 if (p && *p)
782 {
783 char c=0;
784 int col=0;
785 int lineNr=startLine;
786 // skip until the opening bracket or lonely : is found
787 char cn=0;
788 while (*p && !found)
789 {
790 int pc=0;
791 while ((c=*p++)!='{' && c!=':' && c!='=' && c!=0)
792 {
793 //printf("parsing char '%c'\n",c);
794 if (c=='\n')
795 {
796 lineNr++;
797 col = 0;
798 }
799 else if (c=='\t')
800 {
801 col+=tabSize - (col%tabSize);
802 }
803 else if (pc=='/' && c=='/') // skip single line comment
804 {
805 while ((c=*p++)!='\n' && c!=0);
806 if (c == '\n')
807 {
808 lineNr++;
809 col = 0;
810 }
811 }
812 else if (pc=='/' && c=='*') // skip C style comment
813 {
814 while (((c=*p++)!='/' || pc!='*') && c!=0)
815 {
816 if (c == '\n')
817 {
818 lineNr++;
819 col = 0;
820 }
821 pc=c;
822 }
823 }
824 else
825 {
826 col++;
827 }
828 pc = c;
829 }
830 if (c==':')
831 {
832 cn=*p++;
833 if (cn!=':') found=true;
834 }
835 else if (c=='=')
836 {
837 cn=*p++;
838 if (cn=='>') // C# Expression body
839 {
840 found=true;
841 }
842 }
843 else if (c=='{')
844 {
845 found=true;
846 }
847 else if (c==0)
848 {
849 break;
850 }
851 }
852 //printf(" -> readCodeFragment(%s,%d,%d) lineNr=%d\n",fileName,startLine,endLine,lineNr);
853 if (found)
854 {
855 // For code with more than one line,
856 // fill the line with spaces until we are at the right column
857 // so that the opening brace lines up with the closing brace
858 if (endLine!=startLine)
859 {
860 DString spaces;
861 spaces.fill(' ',col);
862 result+=spaces;
863 }
864 // copy until end of line
865 if (c) result+=c;
866 startLine=lineNr;
867 if (c==':' || c=='=')
868 {
869 result+=cn;
870 if (cn=='\n') lineNr++;
871 }
872 char lineStr[blockSize];
873 do
874 {
875 //printf("reading line %d in range %d-%d\n",lineNr,startLine,endLine);
876 int size_read=0;
877 do
878 {
879 // read up to blockSize-1 non-zero characters
880 int i=0;
881 while ((c=*p) && i<blockSize-1)
882 {
883 lineStr[i++]=c;
884 p++;
885 if (c=='\n') break; // stop at end of the line
886 }
887 lineStr[i]=0;
888 size_read=i;
889 result+=lineStr; // append line to the output
890 } while (size_read == (blockSize-1)); // append more if line does not fit in buffer
891 lineNr++;
892 } while (*p);
893
894 // strip stuff after closing bracket
895 size_t newLineIndex = result.rfind('\n');
896 size_t braceIndex = result.rfind('}');
897 if (newLineIndex!=DString::npos && braceIndex!=DString::npos && braceIndex > newLineIndex)
898 {
899 result.resize(braceIndex+1);
900 }
901 endLine=lineNr-1;
902 }
903 if (usePipe)
904 {
905 Debug::print(Debug::FilterOutput, 0, "Filter output\n");
906 Debug::print(Debug::FilterOutput,0,"-------------\n{}\n-------------\n",result);
907 }
908 }
909 DString encoding = getEncoding(FileInfo(fileName.str()));
910 if (encoding!="UTF-8")
911 {
912 std::string encBuf = result.str();
913 bool ok = transcodeCharacterStringToUTF8(encBuf,encoding.data());
914 if (ok)
915 {
916 result = encBuf;
917 }
918 else
919 {
920 err("failed to transcode characters in code fragment in file {} lines {} to {}, from input encoding {} to UTF-8\n",
921 fileName,startLine,endLine,encoding);
922
923 }
924 }
925 if (!result.empty() && result.at(result.length()-1)!='\n') result += "\n";
926 //printf("readCodeFragment(%d-%d)=%s\n",startLine,endLine,qPrint(result));
927 return found;
928}
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
void resize(size_t newlen)
Definition dstring.h:209
size_t rfind(char c, size_t pos=npos) const
Definition dstring.h:244
DString fill(char c, size_t len)
Fills a string with a predefined character.
Definition dstring.h:278
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
static constexpr size_t npos
value used to indicate 'not found' or 'to the end of the string', matching std::string::npos
Definition dstring.h:178
char & at(size_t i)
Returns a reference to the character at index i.
Definition dstring.h:686
const std::string & str() const
Definition dstring.h:645
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition dstring.h:157
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:151
@ FilterOutput
Definition debug.h:39
static void print(DebugMask mask, int prio, fmt::format_string< Args... > fmt, Args &&... args)
Definition debug.h:78
Minimal replacement for QFileInfo.
Definition fileinfo.h:26
bool getFileContents(const DString &fileName, size_t startLine, size_t endLine, std::string &str)
static FilterCache & instance()
#define Config_getInt(name)
Definition config.h:34
#define Config_getBool(name)
Definition config.h:33
#define err(fmt,...)
Definition message.h:127
SrcLangExt
Definition types.h:207
bool transcodeCharacterStringToUTF8(std::string &input, const char *inputEncoding)
Recodes the input string from the given input encoding to UTF8.
Definition utf8.cpp:244
SrcLangExt getLanguageFromFileName(const DString &fileName, SrcLangExt defLang)
Definition util.cpp:4168
DString getFileFilter(const DString &name, bool isSourceCode)
Definition util.cpp:1020
DString getEncoding(const FileInfo &fi)
Definition util.cpp:4477

References DString::at(), Config_getBool, Config_getInt, DString::data(), DString::empty(), err, DString::fill(), Debug::FilterOutput, getEncoding(), FilterCache::getFileContents(), getFileFilter(), getLanguageFromFileName(), FilterCache::instance(), DString::length(), DString::npos, Debug::print(), DString::resize(), DString::rfind(), DString::str(), and transcodeCharacterStringToUTF8().

Referenced by VhdlDocGen::createFlowChart(), DefinitionMutable::toDefinition_(), and DefinitionImpl::writeInlineCode().

◆ toDefinition()

◆ toDefinitionMutable()

DefinitionMutable * toDefinitionMutable ( Definition * d)