Doxygen
Loading...
Searching...
No Matches
definition.h File Reference
#include <vector>
#include "types.h"
#include "reflist.h"
#include "construct.h"
#include "requirement.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 764 of file definition.cpp.

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

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

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

◆ toDefinition()

Definition * toDefinition ( DefinitionMutable * dm)

◆ toDefinitionMutable()

DefinitionMutable * toDefinitionMutable ( Definition * d)